Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you Delete from Firebase Storage using Angularfire2

I am basically new to Angularfire2, I am trying to delete a folder from Firebase Storage when the user presses the delete button. Unfortunately, such information is not in the Angularfire2 documentation.

I have basically tried to use the code below:

constructor(private afs: AngularFirestore, private storage: AngularFireStorage) { 
      this.clientsCollection = this.afs.collection('clients', ref => ref.orderBy('uid', 'asc'));;
  }

   deleteClient(client, callback){
    this.storage.ref(client.uid).delete();
  }

Unfortunately, it throws the following error

FirebaseStorageError {code_: "storage/object-not-found", message_: "Firebase Storage: Object '3CqyNHrIwQ3sRlj83JKM' does not exist.", serverResponse_: "{↵  "error": {↵    "code": 404,↵    "message": "Not Found.  Could not delete object"↵  }↵}", name_: "FirebaseError"}

I have stored each client's documents in a folder which has the name similar to the client's uid. Which is really just an ID generated by Firebase. I get the above error when I try to delete that folder. Please help, how do I go about this?

like image 832
Kelvin Muza Avatar asked Apr 12 '18 16:04

Kelvin Muza


1 Answers

I think a better way would be to delete a file via its download URL. To do that, you can simply call storage.refFromURL(url).delete() on the instance of the injected AngularFireStorage dependency.

...
constructor(private storage: AngularFireStorage) { }
...
delete(downloadUrl) {
  return this.storage.storage.refFromURL(downloadUrl).delete();
}
...
like image 145
SiddAjmera Avatar answered Oct 25 '22 23:10

SiddAjmera