Created
November 7, 2024 15:21
-
-
Save glauesppen/13c009378476cffd0482ff3838e95c16 to your computer and use it in GitHub Desktop.
Deleting a folder in azure blob storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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