I have a Python app. In this context I want to retrieve the blob references from an Azure Storage container that match a certain prefix and then delete all the blobs in one go. I tried the following:
container_client: ContainerClient = ContainerClient.from_connection_string(conn_str=storage_account_connection_string, container_name=container_name)
blob_list: ItemPaged[BlobProperties] = container_client.list_blobs(name_starts_with=prefix)
container_client.delete_blobs(*blob_list, delete_snapshots="include")
This works fine as long as there are blobs that match the prefix. But if that is not the case I get an exception when trying to execute delete_blobs
:
tuple index out of range
I don't want to work with try except and I also don't want to iterate first. I would like to have an indicator that tells me if there are blobs at all without the need to do extra calls.
How can I do that?
Thanks
EDIT: Based on what has been suggested by @Gaurav the following approach works:
from azure.storage.blob import ContainerClient, BlobProperties
from azure.core.paging import ItemPaged
from typing import List
blob_paged: ItemPaged[BlobProperties] = container_client.list_blobs(name_starts_with=prefix)
blob_list: List[dict] = list(blob_paged)
number_of_blobs: int = len(blob_list)
if number_of_blobs > 0:
container_client.delete_blobs(*blob_list, delete_snapshots="include")
log.debug(f"Deleted '{ number_of_blobs }' blobs and snapshots...")
else:
log.debug(f"No blobs to be deleted...")
Three things you should be aware about:
- Using list() will resolve the iterator and load all blobs into memory
- blob_paged can't be used anymore as argument for delete_blobs after being resolved
- When using blob_list as argument for delete_blobs it will log a warning like
Failed to parse headers...
(Bug?). The blobs still get deleted.