Firebase Storage-How to delete file from storage with node.js?
Asked Answered
P

4

6

I want to delete a folder in firebase storage with node js because this is a firebase function.

For example :

storageRef.child(child1).child(child2).delete();

something like this, but firebase documentation doesn't tell anything.

One more question: When initialize storage documentation node js requires my admin json, but realtime database doesn't want this wonder why?

Peewit answered 8/1, 2019 at 10:12 Comment(4)
There is no way to delete a folder from Firebase Storage. See #38214552. To delete all files in the folder, you will need to know what files there are as there is no way to get a list of files in a folder through the Firebase SDK, see: #37335602Dunne
@FrankvanPuffelen But i can delete folders in browser.Is this something related to firebase sdk?Peewit
Correct. The Firebase SDKs don't allow deleting folders.Dunne
@FrankvanPuffelen I just realized something.There are 6-8 items in my storage.10 with folders. And i always deleted foldrs in browser. But when i look at my storage usage it says 230 items in total.So In browser deleting folders also doesnt work but just make folder invisible?Peewit
U
7

You can do it like this using Node.js:

const firebase = require('firebase-admin');

async function deleteImageFromFirebase(imageName) {
    await firebase.storage().bucket().file("folderName/"+imageName).delete();
}

And like this client side:

// Create a reference to the file to delete
var desertRef = storageRef.child('images/desert.jpg');

// Delete the file
desertRef.delete().then(function() {
  // File deleted successfully
}).catch(function(error) {
  // Uh-oh, an error occurred!
});

View this info on the Firebase website: how to delete files Firebase-storage

Ulibarri answered 26/10, 2020 at 15:19 Comment(3)
The question was around deleting a folder. Your answer is simply deleting a file.Scheller
@bjorni just remove the image name from the network call.Ulibarri
Folder must be empty for that to work, @DustinSpenglerFluidextract
D
5

Have a look at the Node.js client API Reference for Google Cloud Storage and in particular at the delete() method for a File.

Disaccord answered 8/1, 2019 at 13:34 Comment(1)
But it asks me to enable billing.Why is that? I use firebase-functions to delete -write data to firebase realtime database it works fine.Peewit
H
1

This might be late but at least on Web (so basically what you need), there is new API to delete the whole folder.

I tested deleting a folder with 2 pictures inside and it works. I then tried a folder-A with contents: folder-B + picture-A. Folder-B also has a picture-B inside; it still deleted folder-A with all of its contents.

Solution:

const bucket = admin.storage().bucket();

return bucket.deleteFiles({
  prefix: `posts/${postId}`
);

I couldn't find this on the official documentation (perhaps is really new API) but really cool article where I found the solution: Automatically delete your Firebase Storage Files from Firestore with Cloud Functions for Firebase

Hadlee answered 8/5, 2019 at 19:35 Comment(4)
seriously this is a bad idea, if by mistake u pass an empty string to it as prefix do u know what happens ? all the files in that bucket ll be deletedFlaw
YAH! THIS JUST )(*$@*(#$# HAPPENED TO ME! I didn't give it an empty string though. I literally had the path to the folder I wanted to delete.... Dear god.....Protero
the link you have shared is for typescript anyone has for js?Raving
I mean you (@Harkal) are not wrong, but why assume that you don't perform validation before executing such operations.Hadlee
E
0
import { storage } from "./firebaseClient";
import { bucket } from "./firebaseServer";

//Let's assume this is the URL of the image we want to delete
const downloadUrl =  "https://storage.googleapis.com/storage/v1/b/<projectID>.appspot.com/o/<location>?"

//firebase delete function
const deleteImages = async ({ downloadUrl }) => {
    const httpsRef = storage.refFromURL(downloadUrl).fullPath;
    return await bucket
        .file(httpsRef)
        .delete()
        .then(() => "success")
        .catch(() => "error")
}

//call the deleteImages inside async function
const deleteStatus = await deleteImages({ downloadUrl: oldImage });
console.log(deleteStatus)  //=> "success"
Eosinophil answered 16/11, 2020 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.