Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseStorage: How to Delete Directory

FirebaseStorage always returns error 400 when I try to delete a directory i.e. something like the following always returns error 400.

let storageRef = FIRStorage.storage().reference().child("path/to/directory") storageRef.deleteWithCompletion { (error) in     print("error: \(error)") // always prints error code 400 } 

However, deleting a file works fine e.g. something like doesn't return an error:

let storageRef = FIRStorage.storage().reference().child("path/to/file.jpg") storageRef.deleteWithCompletion { (error) in     print("error: \(error)") // works fine, error is nil } 

What could I be doing wrong here? I don't reckon it's not supported by FirebaseStorage because deleting files from a directory one by one would be pretty lame (specially if the said directory has 100s or 1000s of these).

like image 256
ishaq Avatar asked Jun 10 '16 13:06

ishaq


People also ask

Can I delete a folder in Firebase storage?

After uploading files to Cloud Storage, you can also delete them. Note: By default, a Cloud Storage bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to allow unauthenticated access.

How do I delete files from storage?

So you should be able to free up some storage space by deleting these unnecessary files. You'll find your downloads folder -- which might be called My Files -- in your app drawer. Tap and hold a file to select it, then tap the trash can icon, the remove button or the delete button to get rid of it.


1 Answers

From the context of a secure google cloud function - you can delete an entire directory using the Google Cloud Storage npm package (aka Google Cloud Storage API) like so:

const gcs = require('@google-cloud/storage')(); const functions = require('firebase-functions'); ...   const bucket = gcs.bucket(functions.config().firebase.storageBucket);    return bucket.deleteFiles({     prefix: `users/${userId}/`   }, function(err) {     if (err) {       console.log(err);     } else {       console.log(`All the Firebase Storage files in users/${userId}/ have been deleted`);     }   }); 

more documentation available on GCS API docs

like image 72
TheFastCat Avatar answered Oct 02 '22 21:10

TheFastCat