Skip to content

Instantly share code, notes, and snippets.

@glauesppen
Created November 7, 2024 15:21
Show Gist options
  • Save glauesppen/13c009378476cffd0482ff3838e95c16 to your computer and use it in GitHub Desktop.
Save glauesppen/13c009378476cffd0482ff3838e95c16 to your computer and use it in GitHub Desktop.
Deleting a folder in azure blob storage
# Thanks to Sumarigo-MSFT
# https://learn.microsoft.com/en-us/answers/questions/1105984/delete-folder-in-storage-account-through-sdk
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
account_url = "https://{container}.blob.core.windows.net"
default_credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url, credential=default_credential)
containername = 'XXX'
foldername = 'XXX'
def delete_folder(foldername):
folders = [blob.name for blob in container_client.list_blobs(name_starts_with=foldername)]
folders.sort(reverse=True, key=len)
if len(folders) > 0:
for folder in folders:
blob_client.delete_blob(folder)
print("deleted folder",folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment