3

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?

KENdi
  • 7,205
  • 2
  • 14
  • 26
UzaySan
  • 486
  • 3
  • 13
  • There is no way to delete a folder from Firebase Storage. See https://stackoverflow.com/questions/38214052/delete-folder-with-contents-from-firebase-storage. 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: https://stackoverflow.com/questions/37335102/how-to-get-a-list-of-all-files-in-cloud-storage-in-a-firebase-app – Frank van Puffelen Jan 08 '19 at 14:31
  • @FrankvanPuffelen But i can delete folders in browser.Is this something related to firebase sdk? – UzaySan Jan 08 '19 at 14:35
  • Correct. The Firebase SDKs don't allow deleting folders. – Frank van Puffelen Jan 08 '19 at 14:52
  • @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? – UzaySan Jan 08 '19 at 15:00

4 Answers4

4

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

Renaud Tarnec
  • 53,666
  • 7
  • 52
  • 80
  • 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. – UzaySan Jan 08 '19 at 14:14
0

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

  • 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 deleted – Harkal Jan 29 '20 at 13:41
  • 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..... – wesley franks Jul 27 '20 at 07:39
  • the link you have shared is for typescript anyone has for js? – Jerin Aug 25 '20 at 19:11
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"
Maduekwe Pedro
  • 1,673
  • 2
  • 13
  • 28
-1

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

Dustin Spengler
  • 2,321
  • 1
  • 19
  • 30