How to delete a file using Cloud Functions?

For anyone else still head-scratching, I was able to get mine deleted by excluding the bucket name (and hence selecting the default firebase bucket):

 const bucket = admin.storage().bucket();
 const path = "path/to/file.wav";
 return bucket.file(path).delete();

You can use the firebase functions environment config to get the bucket name:

const bucket = gcs.bucket(functions.config().firebase.storageBucket)

I have managed to fix it. The Issue here was that I was writing my bucket address wrong; it should be something like postsapp-12312.appspot.com instead of postsapp-12312

Update For a better way to put your bucket address check @Robert answer


take a look at this functions I have used

const app = admin.initializeApp();

export const onDeleteItem = 
 functions.firestore.document('collectionItems/{collectionID}/items/{itemID}')
   .onDelete((snap, context) => {
     const { collectionID } = context.params;
     const { itemID } = context.params
     return deleteItemImage(collectionID, itemID)
     }
 )

async function deleteItemImage(collectionID: string, itemID: string) {
  const path = `images/${collectionID}/${itemID}`;
  const bucket = app.storage().bucket();
  return bucket.file(path).delete()
    .then(function () {
      console.log(`File deleted successfully in path: ${path}`)
    })
    .catch(function (error) {
      console.log(`File NOT deleted: ${path}`)
    })
}