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

Viewed 552

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!

3 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.

What about this solution

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference mStorageRef = 
    storage.getReference().child("fotos_usuarios/"+id);
mStorageRef.listAll().addOnSuccessListener(listResult -> {
    for (StorageReference item : listResult.getItems()) {
        if (item.getName().contains("SOME_WORD")){
            item.delete();
        }
    }
})
Related