1

I am trying to delete content within a folder in firebase storage:

I attempt to do so with the following function:

            let storageRef = Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child("post:\(selectedPost.media[0].postID!)/")
//        let postRef = storageRef.child("image.png")

        // Delete the file
        storageRef.delete { error in
            if error != nil {
                // Uh-oh, an error occurred!
                print("FAIL DELETING POST FROM STORAGE")
            } else {
                // File deleted successfully
                print("SUCCESS DELETING STORAGE REF TO POST!")
            }
        }

The problem is that it does not do that. It prints that it has failed:

    Optional(Error Domain=FIRStorageErrorDomain Code=-13010 "Object LWzvQXAoX3Ov9DW9LopWFFjJBJY2/post:579755726 does not exist." UserInfo={object=LWzvQXAoX3Ov9DW9LopWFFjJBJY2/post:579755726, ResponseBody={
  "error": {
    "code": 404,
    "message": "Not Found.  Could not delete object",
    "status": "DELETE_OBJECT"
  }
}

When checking in the storage I can clearly see that that folder does exist.

What is wrong?

1 Answers1

5

Firestore client SDKs don't offer an operation to delete an entire folder.

In actuality, there are not actually any "folders" in Cloud Storage. There are just object names that look like they contain folders. This is just to help you organize your content so you don't have to see the entire list of files in the console.

What you'll want to do is store all the names of the files somewhere else, such as a database, then query the database to get the names when it's time to delete them. Or you can delete the entire set of files manually with gsutil. You won't be able to do this from the mobile client until the SDKs offer a list API that let you list object with a common prefix. The Firebase team is working on this, but there is no timeline right now.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • From what I see [https://stackoverflow.com/questions/23900943/remove-all-files-under-a-folder-google-cloud-storage] gsuit is used in command line. i cant do that of course every time a user deletes content. Does that mean than that I have to go with the first option you named? Could you show a small snipit on how that may loop in terms of the code I provided –  May 17 '19 at 03:30
  • *work............ –  May 17 '19 at 03:37
  • I can't show you any code, because I don't know how you intend to store all the names of the objects. You have to figure that out for yourself. – Doug Stevenson May 17 '19 at 03:48