Crystal
Crystal

Reputation: 318

Cloud Functions Delete a Folder: TypeScript

My storage bucket budget-32bfc contain the following folders;
--+ Folder1 --+ Folder2 --+ Folder3

I want to delete Folder1 from Cloud Functions.

ATTEMPT 1:

gcs.bucket("budget-32bfc").file('Folder1').delete().then(() => {
        console.log("Folder Deleted");
    }).catch(function (error) {
        console.log("Error listing users:", error);
    });

ATTEMPT 2:

admin.bucket("budget-32bfc").file('Folder1').delete().then(() => {
        console.log("Folder Deleted");
    }).catch(function (error) {
        console.log("Error listing users:", error);
    });

Both attempt gave me a 404 Error: 'No such object: budget-32bfc/Folder1'.

How do I delete a folder??

Upvotes: 2

Views: 1367

Answers (2)

Marius Mircea
Marius Mircea

Reputation: 89

Version 1.7.x has the following method which returns a Promise:

deleteFiles(query, callback)

It will try to delete each file inside the specified folder (folders too). If the deletion of one file fails, the process is stopped. (use 'force: true' to override this behavior).

bucket.deleteFiles({
    prefix: `${userId}/images`
})
.catch( (err) => {
    console.log(`Failed to delete all images of user ${userId}`);
});

Official Documentation: Storage - deleteFiles

Another solution found here: click

(Note: I cannot mark this thread as duplicate as I only have 13 rep points, please stop deleting my comment because others might need to see this, thank you)

Upvotes: 4

Frank van Puffelen
Frank van Puffelen

Reputation: 598785

There is no concept of a "folder" in Cloud Storage, it is merely part of the name of each file. So you will have to delete all files that start with Folder1/. Once you do that, the folder is also gone.

Also see:

Upvotes: 3

Related Questions