Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
            }
        });
like image 436
Tim Daiber Avatar asked Jul 14 '17 12:07

Tim Daiber


People also ask

How to delete file from the firebase using file URL in node?

How to delete file from the firebase using file url in node.js ? To delete a file from the firebase storage we need a reference to store the file in storage. As we only have the file URL we need to create a reference object of the file in firebase storage and then delete that file.

How do I use Firebase Storage?

Firebase Storage is a service provided by Google that allows you to save files on their server. The free plan allows you to upload data up to 1 GB. More data can be bought from the Firebase Pricing page. Upload files on Firebase Storage. Save the data in Realtime Database. Fetch files from the Firebase Storage. Download files from Firebase Storage.

How do I delete an image from Firebase?

Using 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. And that’s how to upload images, retrieve and display them, and delete them!

How to delete data from Firebase Storage in Vue JS?

Now you need to create the following function in your Javascript: This will first delete the data from the real-time database. Then it will delete the file from Firebase Storage. Finally, it will remove it from the Vue JS array, so it will automatically be removed from the HTML table too.


3 Answers

 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");
    } 
}); 
like image 127
Vivek Hirpara Avatar answered Oct 11 '22 19:10

Vivek Hirpara


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
    }
});
like image 38
Nikunj Paradva Avatar answered Oct 11 '22 20:10

Nikunj Paradva


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");
         }
      });
like image 29
akhilesh0707 Avatar answered Oct 11 '22 20:10

akhilesh0707