Reputation: 61
I would like do delete an image from Firebase Storage.
I tried this but it's not working for me
await _storage.child('gs://flutter-firebase-636d0.appspot.com/e-commerce/header_image/img1.jpg').delete();
Upvotes: 3
Views: 3738
Reputation: 1481
Now you can delete the image with download URL easily
String imageUrl = 'here goes image url';
StorageReference ref = await FirebaseStorage().getReferenceFromUrl(imageUrl);
ref.delete();
Update
FirebaseStorage.instance.refFromURL(imageUrl).delete();
Upvotes: 5
Reputation: 59
Feb 2021:
Future<void> removeImageFromUrl(String url) async {
try {
Reference ref = await FirebaseStorage.instance.refFromURL(url);
await ref.delete();
} catch (e) {
print('Failed with error code: ${e.code}');
print(e.message);
}
}
The url in the above method is the one that is fetched after storing the image to the bucket.
eg:
https://firebasestorage.googleapis.com/v0/b/xxxx.appspot.com/o/bucket%2xxxx?alt=media&token=xxxx
Reference: https://pub.dev/documentation/firebase_storage/latest/firebase_storage/Reference-class.html
Upvotes: 1
Reputation: 4387
You can't put the gs://
url inside the child method as a parameter. However, you can feed this to the storageBucket
property of the FirebaseStorage class.
final FirebaseStorage storage = FirebaseStorage(storageBucket: 'gs://flutter-firebase-636d0.appspot.com/');
Then you can delete your image like this:
onPressed: () async{
await storage.ref().child("e-commerce/header_image/img1.jpg").delete();
}
Upvotes: 4
Reputation: 18612
You need to pass the path to the file as the argument of the child like this:
await _storage.child('sample/path/to/the/image.jpg').delete();
Upvotes: 0