Home > Software design >  How can I remove an entire folder under Storage?
How can I remove an entire folder under Storage?

Time:08-29

I have a problem deleting my entire folder on firebase storage. I always get a 'storageRef.listAll is not a function' error when I specify the reference to the path. I would like to delete the complete path with all possibly existing images.

const storage = getStorage();

const storageRef = ref(
  storage,
  `${aktuellerUser._id}/artikelBilder/`
);

storageRef
  .listAll()
  .then((listResults) => {
    const promises = listResults.items.map((item) => {
      return item.delete();
    });
    Promise.all(promises);
  })
  .catch((error) => {
    console.log(error);
  });

error TypeError: storageRef.listAll is not a function

enter image description here

CodePudding user response:

You are using the new Firebase Modular SDK where listAll() is a top level function and not a method on StorageReference. Similarly, you need to use deleteObject() to delete a file now instead of .delete() method as shown below:

import {
  getStorage,
  ref,
  listAll,
  deleteObject
} from "firebase/storage";

listAll(storageRef)
  .then(async (listResults) => {
    const promises = listResults.items.map((item) => {
      return deleteObject(item);
    });

    await Promise.all(promises);
  })
  • Related