2

I want to delete a Firebase Storage folder and all its contents using node.js / Firebase Admin SDK but I'm not able to.

A similar question was asked in the google group below about a year ago and I'm wondering if there is a solution now:

https://groups.google.com/forum/#!topic/firebase-talk/aG7GSR7kVtw

I am able to delete a single file using node.js example below:

https://mzmuse.com/blog/how-to-upload-to-firebase-storage-in-node

But I'm not able to delete a folder and all it's contents.

Any ideas? Am I missing something?

Here's the code I'm using

const keyFilename="path/to/my/private.json";
const projectId = "myprojectid";
const bucketName = `${projectId}.appspot.com`;

const gcs = require('@google-cloud/storage')({
    projectId,
    keyFilename
});
const bucket = gcs.bucket(bucketName);

THIS WORKS FINE - Deleting a single file

const deleteFile = 'users/user1/folder1/IMG_1.JPG'
const gcFile = bucket.file(deleteFile);
gcFile.delete((err,res)=>console.log(err||res));

THIS DOES NOT WORK - Deleting the folder and contents

const deleteFolder = 'users/user1/'
const gcFolder = bucket.file(deleteFolder);
gcFolder.delete((err,res)=>console.log(err||res));

--

THIS IS NOT A DUPE AS MARKED BY SOME MEMBERS

My question is specific to node.js and the answer given is for Java.

UPDATE

I found this a page in google cloud site where they show a way to delete all files under a directory (folder)

https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.56.0/storage/bucket?method=deleteFiles

bucket.deleteFiles({
  prefix: 'images/'
}, function(err) {
  if (!err) {
    // All files in the `images` directory have been deleted.
  }
});

But I'm still not able to delete the folder itself

Luis Cabrera
  • 569
  • 1
  • 7
  • 18
  • You can't do this programmatically. Read the answer on the dup to understand why there are not actually any "folders" in a Cloud Storage bucket. Currently, the only way to delete all the files under a location is with the gsutil command line. – Doug Stevenson Jul 16 '17 at 20:44
  • While these are Swift questions, the answers are applicable and contain really good information that answers the question: [This One](https://stackoverflow.com/questions/38214052/delete-folder-with-contents-from-firebase-storage) and [Another One](https://stackoverflow.com/questions/37749647/firebasestorage-how-to-delete-directory) and [This Google Group](https://groups.google.com/forum/#!topic/firebase-talk/aG7GSR7kVtw) – Jay Jul 20 '17 at 18:31
  • 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:28

1 Answers1

1

From this post:

You can delete only the whole bucket, but you cannot delete a folder in a bucket.

Use GcsService to delete files or folders.

String bucketName = "bucketname.appspot.com";

GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
gcsService.delete(new GcsFilename(bucketName, "test"));
coderpc
  • 4,119
  • 6
  • 51
  • 93