How to delete a folder from Firebase Storage with Flutter

302

Folders in Storage are automatically created when you add the first file to them, and automatically deleted when you delete the last file from them.

So the only way to get rid of the entire folder of files for your user, is to delete each individual file from it..

Share:
302
fryn4538
Author by

fryn4538

Updated on December 29, 2022

Comments

  • fryn4538
    fryn4538 over 1 year

    I am creating a Flutter app that uses Firebase storage. I have the structure of a collection with the user data in Firestore and a folder on the Firebase storage with named after the uid of the current user. However if the user want to delete the account all the images have to be removed. Is there a way to remove an entire directory in Firebase storage through Flutter?

    The files are uploaded as such:

    // Upload img
    firebase_storage.Reference storageRef = firebase_storage
        .FirebaseStorage.instance
        .ref()
        .child('images/users/' + uid + '/mealImages/' + fileName);
    firebase_storage.UploadTask uploadTask = storageRef.putFile(imageFile); 
    
    // Remove img where uid=aaa and fileName=bbb.jpg
    await firebase_storage.FirebaseStorage.instance
        .ref()
        .child(
            'images/users/aaa/mealImages/bbb.jpg')
        .delete();
    
  • fryn4538
    fryn4538 about 3 years
    We create the folders with the following code: firebase_storage.Reference storageRef = firebase_storage .FirebaseStorage.instance .ref() .child('images/users/' + uid + '/mealImages/' + fileName); firebase_storage.UploadTask uploadTask = storageRef.putFile(imageFile); Lets say that the uid=aaa and fileName=bbb.jpg then I try to remove the file with: await firebase_storage.FirebaseStorage.instance.ref().child('image‌​s/users/aaa/mealImag‌​es/bbb.jpg').delete(‌​); It removes the file but the folder is still there. Should the folder be created in any other way?
  • fryn4538
    fryn4538 about 3 years
    Found now that the reason that it didn't work before was because I created the folder via the firebase console.