FirebaseStorage: How to Delete Directory

You can list a folder and delete its contents recursively:

deleteFolder(path) {
  const ref = firebase.storage().ref(path);
  ref.listAll()
    .then(dir => {
      dir.items.forEach(fileRef => this.deleteFile(ref.fullPath, fileRef.name));
      dir.prefixes.forEach(folderRef => this.deleteFolder(folderRef.fullPath))
    })
    .catch(error => console.log(error));
}

deleteFile(pathToFile, fileName) {
  const ref = firebase.storage().ref(pathToFile);
  const childRef = ref.child(fileName);
  childRef.delete()
}

From the context of a secure google cloud function - you can delete an entire directory using the Google Cloud Storage npm package (aka Google Cloud Storage API) like so:

const gcs = require('@google-cloud/storage')();
const functions = require('firebase-functions');
...
  const bucket = gcs.bucket(functions.config().firebase.storageBucket);

  return bucket.deleteFiles({
    prefix: `users/${userId}/`
  }, function(err) {
    if (err) {
      console.log(err);
    } else {
      console.log(`All the Firebase Storage files in users/${userId}/ have been deleted`);
    }
  });

more documentation available on GCS API docs


from google group, deleting a directory is not possible. You have to maintain a list of files somewhere (in Firebase Database) and delete them one by one.

https://groups.google.com/forum/#!topic/firebase-talk/aG7GSR7kVtw

I have also submitted the feature request but since their bug tracker isn't public, there's no link that I could share.


In 2020, deleting Firebase Storage folder seems to just work. I just did, in my Cloud Functions (Node.js Admin), below and it deletes folder, sub-folders, and files in them. I did download Google Cloud Storage using npm, but I didn't import that library in anyway physically, and it seems Firebase Storage is now supporting this feature unlike what everyone said above. Maybe they have updated it. I tested it that it is working.

admin.initializeApp({
    storageBucket: "bucket_name.appspot.com",
})

const bucket = admin.storage().bucket();
await bucket.deleteFiles({
   prefix: `${folderName}/`
});