Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete image from Firebase Storage?

Can you help delete an image from Firebase Storage. String deleteImage holds the full url of where the image is located in the Firebase storage.

My code is as follows, but it does not delete the image:

StorageReference deleteFile = storageReference.child(deleteImage);
            deleteFile.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(EditProfile.this, "Previous Image Deleted", Toast.LENGTH_SHORT).show();
                }
            });
like image 316
Jama Mohamed Avatar asked Mar 21 '17 14:03

Jama Mohamed


People also ask

How do I delete all photos from Firebase Storage?

Delete files from FirebaseUsing refFromURL() , get the image reference from Firebase Storage of the image that should be deleted. Then use . delete() to delete the image from Firebase. Finally, remove that URL from the allImages array.

How do I delete photos from Firebase Storage Kotlin?

To delete a file, first create a reference. to that file. Then call the delete() method on that reference.


2 Answers

You need to use this method call:

StorageReference photoRef = mFirebaseStorage.getReferenceFromUrl(mImageUrl);

Then delete as you were:

photoRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // File deleted successfully
        Log.d(TAG, "onSuccess: deleted file");
    }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
        Log.d(TAG, "onFailure: did not delete file");
    }
});
like image 159
Michael Vescovo Avatar answered Oct 05 '22 04:10

Michael Vescovo


Use the function getReferenceFromUrl(URL)

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
                    StorageReference storageReference = firebaseStorage.getReferenceFromUrl(pd.getUrl());
                    storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Log.e("Picture","#deleted");
                        }
                    });
like image 27
abhijith mogaveera Avatar answered Oct 05 '22 03:10

abhijith mogaveera