How To Delete A File, Delete Folder From Firebase Cloud Storage In Cloud Functions(Nodejs)
To delete a file from Firebase Cloud storage in cloud functions, you need the path to the file. A path is basically the Storage path of the file. You will also need the bucket in which the file is stored depending on your setup.
Setting your firebase admin project:
//javascript
// import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";
// Service account required for Stripe Connect OAuth
const serviceAccount = require('../../credentials.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: [your-bucketname]'
});
export const storage = admin.storage();
To delete a single file at a time:
//javascript
await storage.bucket('[bucket-name]').file('folder-name/thumb_img_1f2d8a81-b366-4380-9014-928b5f9cdb84').delete();
Deleting a complete folder
//javascript
await storage.bucket('[bucket-name]').deleteFiles({
prefix: `k-discussions`
});
The above works for me, hopefully it will for you too!!