Deleting a folder from Firebase Cloud Storage in Flutter

1,857

Solution 1

Cloud Storage doesn't actually have any folders. There are just paths that look like folders, to help you think about how your structure your data. Each object can just have a common prefix that describes its "virtual location" in the bucket.

There's no operations exposed by the Firebase SDKs that make it easy to delete all objects in one of these common prefixes. Your only real option is to list all files in a common prefix, iterate the results, and delete each object individually.

Unfortunately, the list files API has not made it to flutter yet, as discussed here. So you are kind of out of luck as far as an easy solution is concerned. See also: FirebaseStorage: How to Delete Directory

Your primary viable options are:

Solution 2

Currently, I'm working on a project that contains a single file inside every folder so I did this and it worked for me.

await FirebaseStorage.instance.ref("path/" + to + "/" + folder)
                  .listAll().then((value) {
FirebaseStorage.instance.ref(value.items.first.fullPath).delete();
});

This code deletes the file and folder as well. Since, there's no folder here, deleting the file deletes the folder or reference. You can use foreach loop or map to delete everything from the folder if you have multiple files.

Solution 3

MD. Saffan Alvy was right, but to completely delete all and not just one file do this. if you didn't know.

await await FirebaseStorage.instance.ref("users/${FirebaseAuth.instance.currentUser!.uid}/media").listAll().then((value) {
value.items.forEach((element) {
FirebaseStorage.instance.ref(element.fullPath).delete();
);
});
Share:
1,857
user2181948
Author by

user2181948

Updated on December 23, 2022

Comments

  • user2181948
    user2181948 over 1 year

    I have a Flutter mobile app in which I am trying to delete a folder (and its contents) from Firebase Cloud Storage. My method is as follows:

    deleteFromFirebaseStorage() async {
         return await FirebaseStorage.instance.ref().child('Parent folder/Child folder').delete();
    }
    

    I expect Child folder and its contents to be deleted, but this exception is thrown:

    Unhandled Exception: PlatformException(Error -13010, FIRStorageErrorDomain, Object Parent folder/Child folder does not exist.)

    However I can clearly see that folder exists in Cloud Storage. How do I delete this folder?