2

I have a folder in firebase storage where I upload users images to, however I can't delete this folder.

Storage.storage().reference().child("folder").delete(); 

I get error code 404, message: Not found. Could not delete object.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Ken
  • 31
  • 1
  • 4

1 Answers1

9

EDIT:

You can use the new List API to list files in storage with some common prefix. The prefix is effectively the the path where the objects live. You will have to them iterate each object you get from the API, and delete each one individually. Also read this blog post about the API.

ORIGINAL ANSWER:

There is currently no way to programmatically delete an entire folder in Cloud Storage with the Firebase SDK. It turns out, with Cloud Storage, there is not even any "folders" at all. A storage bucket just a collection of objects that have names that look like file paths. It is not a real "filesystem" in this respect.

If you want to delete all the files under a certain path, you will have to find all their names and remove them individually. Typically, applications will store the paths of known objects in Realtime Database for this reason.

If you want to delete all objects under a path from the command line with gsutil, read the docs for "gsutil rm".

danday74
  • 52,471
  • 49
  • 232
  • 283
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 15
    Firebase is turning out to have ALLOT of limitations.... – Ruan Mar 08 '18 at 20:28
  • 1
    @Ruan These limitations are bound to the way that Firebase Firestore & Firestorage work (distribution of files bunches on several servers, etc.). But I totally agree with you. I was working on a project that requires GDPR deletions of profiles, *i.e.* deletion of Firestorage nested collections, which are bound to FirebaseAuth and to FireStorage... Result: impossible to nest callbacks of these 3 tools together, which was required to be sure that all data was correctly deleted, for example. Moreover, even considering only 1 tool, FirebaseStorage *e.g.*, it's impossible to batch deletions... Wtf. – JarsOfJam-Scheduler Jun 29 '19 at 13:27
  • 1
    Btw, people are asking for all these improvements for at least 3 years (last date I remember: 2016). And Google didn't implement such functions. All we can deal with is Python Cloud Functions we must setup server side. But: 1) One doesn't necessarily have a server, nore a Python server..... 2) In some projects, even the big operations must be executed client side (*i.e.:* Android app side, *e.g.:* Firestorage deletions), because the project has been defined by the Project Manager as this.... – JarsOfJam-Scheduler Jun 29 '19 at 13:32
  • So... From the experience I acquired with Firebase...: I will use it only for precise projects (which don't require a lot of nested deletion / deletions of multiple files / etc. ). It's sad. Because Firebase's tools (Storage etc.) allowed me to write less to do more............. But the fact that they made me waste time a lot in the above use cases I listed makes me think they actually should be used for these kinds of projects and not **all** the projects... In other words, from the Project Manager's point of view: Firebase will be used for estimates of less than 5000 euros ($5695).... – JarsOfJam-Scheduler Jun 29 '19 at 13:35
  • As of Jul 2019, you can list a folder, and delete its contents recursively, see my answer here: https://stackoverflow.com/questions/37749647/firebasestorage-how-to-delete-directory/56844189#56844189 – Miki Jul 02 '19 at 17:29
  • https://googleapis.dev/nodejs/storage/latest/Bucket.html#deleteFiles – danday74 Dec 21 '21 at 14:02