0

I am trying to delete all the images from a folder in firebase, or delete the folder itself and all the images that way.

I have looked at the solution from MIKI: FirebaseStorage: How to Delete Directory

I cant get it to work. The images are placed in the bookPictures folder under another folder with the ID of the book.

Folder structure looks like this: Folderstructure in firebase

Here is my code:

this.deleteFolderContents(`/bookPictures/${id}`)


deleteFolderContents = (path) => {

    const ref = firebase.storage().ref(path)
    ref.listAll().then(dir => {
        dir.items.forEach(fileRef => {
            this.deleteFile(ref.fullPath, fileRef.name);
        });
        dir.prefixes.forEach(folderRef => {
            this.deleteFolderContents(folderRef.fullPath);
        })
    })
        .catch(error => {
            console.log(error.message)
        });
}

Update: In the solution from MIKI he uses the deleteFile. I thought it was a firebase function. I logged the error: _this.delete is not a function. (In '_this.delete(ref.fullPath, fileRef.name)', '_this.delete' is undefined)

How do i delete images ?

maje17bi
  • 31
  • 4
  • Your try/catch isn't going to capture errors from the promise chain inside it. Use a catch() callback after then() to see if there's an error, then edit the question to show what that might be. Also, catch error from the promises returned by deleteFile, whatever that might be. – Doug Stevenson Nov 19 '19 at 15:45
  • @DougStevenson i have updated the question – maje17bi Nov 19 '19 at 16:04
  • Please edit the question to show what deleteFile and deleteFolderContents are. The error could be in one of those. – Doug Stevenson Nov 19 '19 at 16:22
  • @DougStevenson Hi again, so I did update the question with what deleteFile is. I thought it was a firebase function, meaning that I haven't created the function and I don't know how to delete the images perhaps u can help me? Furthermore, u can see in the code that deleteFolderContents is the method itself that I've already shown in the code above. As I said I took the code from Miki in the link I've put in the question. – maje17bi Nov 19 '19 at 16:32
  • This is still not fixed, can anyone help me ?? – maje17bi Dec 07 '19 at 11:42

0 Answers0