Tim Daiber
Tim Daiber

Reputation: 358

Deleting file from Firebase Storage using URL

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

Upvotes: 7

Views: 14108

Answers (5)

Shashwat Aditya
Shashwat Aditya

Reputation: 406

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

eg:

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

Upvotes: 2

Thiago Silva
Thiago Silva

Reputation: 796

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

Upvotes: 1

Nikunj Paradva
Nikunj Paradva

Reputation: 16077

Snippet for Delete file from Firebase Storage Using URL:

StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/***********************-5fac-45b6-bbda-ed4e8a3a62ab");
storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // File deleted
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Error
    }
});

Upvotes: 8

Vivek Hirpara
Vivek Hirpara

Reputation: 807

 StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/***********************-5fac-45b6-bbda-ed4e8a3a62ab");
 storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
    public void onSuccess(Void aVoid) {
        // File deleted successfully 
        Log.e("firebasestorage", "onSuccess: deleted file");
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred! 
        Log.e("firebasestorage", "onFailure: did not delete file");
    } 
}); 

Upvotes: 13

akhilesh0707
akhilesh0707

Reputation: 6899

try this I have tried this and its working

 String storageUrl = "Chat-Images/1498804025000.png";
 StorageReference storageReference = FirebaseStorage.getInstance().getReference().child(storageUrl);
 storageReference.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");
         }
      });

Upvotes: 5

Related Questions