Fabio Marzocca
Fabio Marzocca

Reputation: 1677

How to delete all files starting with "foo" in Firebase Storage

I have a long list of files in Firebase Storage, which I have uploaded from a python script. Many of those files have this kind of names:

foo_8346gr.msb
foo_8333ys.msb
foo_134as.mbb
...

I know there is no programmatic way to delete a folder in Storage (they are not even folders), but how could I remove all files starting with "foo_" programmatically, from python?

Upvotes: 0

Views: 942

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317467

You can use Cloud Storage List API to find all files with a certain prefix, then delete them. That page has code samples for a variety of languages, including Python. Here's how you list files with a prefix:

storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)

blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter)

print('Blobs:')
for blob in blobs:
    print(blob.name)

if delimiter:
    print('Prefixes:')
    for prefix in blobs.prefixes:
        print(prefix)

You will have to add the bit of code that deletes the file if you believe it should be deleted. The documentation goes into more detail about the List API.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598837

Firebase provides a wrapper around Cloud Storage that allows you to directly access the files in storage from the client, and that secures access to those files. Firebase does not provide a Python SDK for accessing these files, but since it is built around Google Cloud Storage, you can use the GCP SDK for Python to do so.

There is no API to do a wildcard delete in there, but you can simply list all files with a specific prefix, and then delete them one by one. For an example of this, see the answer here: How to delete GCS folder from Python?

Upvotes: 0

Related Questions