Deleting file from Firebase Storage using URL

Viewed 12957

I am trying to delete a file from Firebase Storage using the files URL. My issue is that the getReferenceFromUrl() can not be resolved.

Sample code here:

 StorageReference mStorageRef;
    String storageurl = "http:sample"
    mStorageRef = FirebaseStorage.getInstance().getReference();
        StorageReference ref2 = mStorageRef.getReferenceFromUrl(storageurl);
        ref2.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // File deleted successfully
                Toast.makeText(getContext(), "file deleted", Toast.LENGTH_SHORT).show();
                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");
            }
        });
5 Answers

In case you are using kotlin, this is the code:

        val storageReference: StorageReference =            FirebaseStorage.getInstance().getReferenceFromUrl(urifinal) //urifinal is a String variable with the url
        storageReference.delete().addOnSuccessListener {
            //File deleted
            Log.d("storage", "Done")
        }.addOnFailureListener {
            //failed to delete
            Log.d("storage", "error while deleting")
        }

I think what you need is getStorage() to be able to use getReferenceFromUrl(),

eg:

FirebaseStorage.getInstance().getStorage().getReferenceFromUrl(fileURL);
Related