Edo Sagron
ย ยทย Flutter Developer

Delete Files From Storage [Firebase]

Hey ๐Ÿ˜

Another custom code I wanted to share!

This one is actually pretty simple, but can be very useful in managing your storage database and deleting unwanted or no longer relevant files from within your app, and lowering your storage bills!

The code I share is WITHOUT the default FlutterFlow imports!

deleteFilesFromStorage action:

import 'package:firebase_storage/firebase_storage.dart';

Future<String> deleteFilesFromStorage(List<String> fileURLs) async {
  try {
    final storage = FirebaseStorage.instance;
    for (final fileURL in fileURLs) {
      await storage.refFromURL(fileURL).delete();
    }
    return 'Success';
  } catch (e) {
    return 'Failed - $e';
  }
}

This action is pretty self explainatory, it accepts a list of strings and deletes each of them from our storage, at the end of the process it returns a message (return value of type String).

The reason I return a string is because I use a snackbar to display the string I get from this action so the user knows if the operation was a success or if it failed.

7
10 replies