Delete Folder With Contents from Firebase Storage

FirebaseStorage: How to Delete Directory

from google group, deleting a directory is not possible. You have to maintain a list of files somewhere (in Firebase Database) and delete them one by one.

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

I have also submitted the feature request but since their bug tracker isn't public, there's no link that I could share.

Delete folder with contents from Firebase Storage

Long story short, we haven't implemented a recursive (or folder) delete. This topic is covered in another post: FirebaseStorage: How to Delete Directory

For now, we recommend either storing a list of files in another source (like the Realtime Database) or using the list API, and then deleting files as necessary.

You can also perform this type of delete in the Firebase Console (console.firebase.google.com).

In the future, we may offer this sort of functionality, but don't have it fully spec'ed out.

How to delete a folder from Firebase Storage with Flutter

Folders in Storage are automatically created when you add the first file to them, and automatically deleted when you delete the last file from them.

So the only way to get rid of the entire folder of files for your user, is to delete each individual file from it..

Deleting a folder from Firebase Cloud Storage in Flutter

Cloud Storage doesn't actually have any folders. There are just paths that look like folders, to help you think about how your structure your data. Each object can just have a common prefix that describes its "virtual location" in the bucket.

There's no operations exposed by the Firebase SDKs that make it easy to delete all objects in one of these common prefixes. Your only real option is to list all files in a common prefix, iterate the results, and delete each object individually.

Unfortunately, the list files API has not made it to flutter yet, as discussed here. So you are kind of out of luck as far as an easy solution is concerned. See also: FirebaseStorage: How to Delete Directory

Your primary viable options are:

  • Keep track of each object in a database, then query the database to get the list of objects to delete.
  • Delete the objects on a backend using one of the server SDKs. For example: Delete folder in Google Cloud Storage using nodejs gcloud api

How to delete an folder from Firebase which contains images?

According to this answer, it's impossible. You have to delete them one by one.

Delete Folder inside Firebase Storage with Cloud Functions

You can try using @google-cloud/storage module and setting the path prefix:

const { Storage } = require("@google-cloud/storage")

exports.listingImagesDelete = functions.firestore.document("listings/{listingId}").onDelete((snapshot, context) => {
const listingId = snapshot.id;

const storage = new Storage();

const bucket = storage.bucket('name'); // [PROJECT_ID].appspot.com is default bucket

return bucket.deleteFiles({
prefix: `ListingImages/${listingId}`
})
});

How do I delete a folder from Firebase Storage using a function?

You can use the getFiles() method of a Bucket, with a GetFilesOptions parameter, in order to loop over all the files, as follows:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.justAnExampleOfCloudFunction = functions.firestore
.document("/triggeringCollection/{docId}")
.onCreate(async (snap, context) => {

const bucket = admin.storage().bucket();
const imagesFilesArray = await bucket.getFiles({ directory: 'images' });
const files = imagesFilesArray[0];

const promises = [];

files.forEach(f => {
promises.push(f.delete());
})

await Promise.all(promises);

return null;

});

How to delete a Firebase Storage folder from a Firebase Cloud Function?

As @Doug already mentioned in the comment, "Firebase just provides wrappers around Cloud Storage. They are the same thing.". Also, according to this documentation, "Cloud Storage for Firebase stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs for Cloud Storage."

Having been said that, I've tried replicating the code snippet you've provided using deleteFiles(), and it worked fine on my end:

// // The Firebase Admin SDK to access Firestore.
const functions = require("firebase-functions");
const admin = require('firebase-admin');

const firebaseConfig = {
// Your Firebase configuration...
};

admin.initializeApp(firebaseConfig);

const bucket = admin.storage().bucket();
async function deleteFolder(){
await bucket.deleteFiles({
prefix: 'images/users/${uid}' // the path of the folder
});
}

deleteFolder();

One another option that you can do is to directly use Google Cloud Storage, and skip using the Firebase Storage:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket("your-bucket-name");

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

Just a note, following the suggestion of Doug, you can try and test it out first in your local or test environment. For further reference, you can refer to delete() and deleteFiles()

Delete Folder from Firebase Storage using Google Cloud Storage

Buckets are the basic containers that hold your data. You have a bucket with name "bucketname.appspot.com". "bucketname.appspot.com/test" is your bucket name plus a folder, so its not a valid name of your bucket. By calling bucket.delete(...) 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"));

How to delete all files in a firebase storage directory

Is there a way to iterate through all the files in a directory and
deleting them one by one?

Yes, you can use the listAll() method, as follows:

  const storageRef = firebase.storage().ref('temp');
storageRef.listAll().then((listResults) => {
const promises = listResults.items.map((item) => {
return item.delete();
});
Promise.all(promises);
});

Note that:

  1. This method is only available for Firebase Rules Version 2 (add rules_version = '2'; at the top of the Security Rules).
  2. This is a helper method for calling list() repeatedly until there are no more results. The default pagination size is 1000.


Related Topics



Leave a reply



Submit