6

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?

Junsu Cho
  • 826
  • 7
  • 16
user2181948
  • 1,646
  • 3
  • 33
  • 60

4 Answers4

6

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();
);
});
PAGE BUNNII
  • 169
  • 2
  • 8
5

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:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Is this still true that you cannot delete a 'folder' within storage? It is unclear if this has been implemented yet or is still a limitation in Cloud Storage. – Dennis Ashford Jun 22 '22 at 15:01
4

I built a script that deletes recursively all files inside a folder and all subfolders, here is:

import 'package:firebase_storage/firebase_storage.dart';

class FirebaseStorageApi {

  static Future<void> deleteFolder({
    required String path
  }) async {
    List<String> paths = [];
    paths = await _deleteFolder(path, paths);
    for (String path in paths) {
      await FirebaseStorage.instance.ref().child(path).delete();
    }
  }

  static Future<List<String>> _deleteFolder(String folder, List<String> paths) async {
    ListResult list = await FirebaseStorage.instance.ref().child(folder).listAll();
    List<Reference> items = list.items;
    List<Reference> prefixes = list.prefixes;
    for (Reference item in items) {
      paths.add(item.fullPath);
    }
    for (Reference subfolder in prefixes) {
      paths = await _deleteFolder(subfolder.fullPath, paths);
    }
    return paths;
  }

}

Usage:

await FirebaseStorageApi.deleteFolder(path: "YOUR/FOLDER/PATH");
1

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.

MD. Saffan Alvy
  • 182
  • 2
  • 15