Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in firebase functions to delete storage files that match a regular expression?

I can't find a way to delete all the files inside firebase storage that match a regular expression. I would like to use something like:

        const bucket = storage.bucket(functions.config().firebase.storageBucket);
        const filePath = 'images/*.png';
        const file = bucket.file(filePath);
        file.delete();

Or similar to be able to delete all files inside "images" with png extension.

I tried searching in Stackoverflow and in the samples repository without luck https://github.com/firebase/functions-samples

Any help would be much appreciated. Thanks!

like image 414
Rafa Avatar asked Oct 13 '25 05:10

Rafa


2 Answers

No. In order to delete a file from Cloud Storage, you need to be able to construct a full path to that file. There are no wildcards or regular expressions.

It's common to store the paths to files in a database in order to easily discover the names of files to delete by using some query.

like image 98
Doug Stevenson Avatar answered Oct 14 '25 17:10

Doug Stevenson


If you want to do this in Cloud Functions, you'll be using the Firebase Admin SDK. And the Cloud Storage functionality in there (admin.storage()) is a thin wrapper around the Cloud Storage SDK for Node.js. So if you search for cloud storage node.js path regular expression you'll get some relevant results.

  • How to get files from Google Cloud Storage bucket based on a regular expression?, which indicates that prefix matching is quite possible with bucket.getFiles({ prefix: 'path/to/file' }....
like image 27
Frank van Puffelen Avatar answered Oct 14 '25 18:10

Frank van Puffelen