How to delete a Firebase Storage file with flutter?

change filePath to

String filePath = 'https://firebasestorage.googleapis.com/v0/b/dial-in-21c50.appspot.com/o/default_images%2Fuser.png?alt=media&token=c2ccceec-8d24-42fe-b5c0-c987733ac8ae'
                  .replaceAll(new 
                  RegExp(r'https://firebasestorage.googleapis.com/v0/b/dial-in-21c50.appspot.com/o/default_images%2F'), '').split('?')[0];

FirebaseStorage.instance.ref().child(filePath).delete().then((_) => print('Successfully deleted $filePath storage item' ));

I have implemented a cleaner code but same as @earyhe, which is using Uri.decodeFull() and Path.basename(). Tried and it worked.

import 'package:path/path.dart' as Path;


  Future<void> deleteImage(String imageFileUrl) async {
  var fileUrl = Uri.decodeFull(Path.basename(imageFileUrl)).replaceAll(new RegExp(r'(\?alt).*'), '');


final StorageReference firebaseStorageRef =
    FirebaseStorage.instance.ref().child(fileUrl);
    await firebaseStorageRef.delete();
 }

Update (2021-05-22):

FirebaseStorage.instance.refFromURL(url).delete() should be used now. getReferenceFromUrl that was suggested earlier seems to be gone from the API.

Update (2019-08-03):

According to this PR there is now a getReferenceFromUrl method in FlutterFire that allows looking up a storage reference by its download URL.


Previous answer:

On Android you can call getReferenceForUrl() to get a StorageReference from a download URL, and a similar method exists on iOS.

But I can't find a corresponding method in the FlutterFire reference docs. This unfortunately means that there is no way to map from a download URL back to a StorageReference in Flutter.

This sounds like an omission, so I recommend casting your +1 on this feature request.

For the moment this means you'll need to have the relative path to the file in Cloud Storage to be able to delete it from Flutter. With that path, your current code would work.