How to delete image from Firebase Storage?

Jama Mohamed picture Jama Mohamed · Mar 21, 2017 · Viewed 30.7k times · Source

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();
                }
            });

Answer

Michael Vescovo picture Michael Vescovo · Mar 25, 2017

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");
    }
});