Delete Folder in Storage Account through SDK

Davi Sena 41 Reputation points
2022-11-28T01:38:19.133+00:00

I need to delete a certain folder. Unfortunately, if I try to use the method delete_blob from azure BlobClient, I get the following exception: "azure.core.exceptions.ResourceExistsError: This operation is not permitted on a non-empty directory."

Could you please provide a python SDK code, that deletes all the contents in a folder. For example, in the following image, I would like to delete all of the contents in the source folder. 264496-pombo.png

PS: also, is there any way to determine whether a blob is a folder or a file? Thank you.

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,599 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sumarigo-MSFT 45,321 Reputation points Microsoft Employee
    2022-11-28T13:48:39.603+00:00

    @Davi Sena In Azure Blob Storage, as such a folder doesn't exist. It is just a prefix for a blob's name. For example, if you see a folder named images and it contains a blob called myfile.png, then essentially the blob's name is images/myfile.png. Because the folders don't really exist (they are virtual), you can't delete the folder directly.

    What you need to do is delete all blobs individually in that folder (or in other words delete the blobs whose name begins with that virtual folder name/path. Once you have deleted all the blobs, then that folder automatically goes away.

    In order to accomplish this, first you would need to fetch all blobs whose name starts with the virtual folder path. For that you will use list_blobs method and specify the virtual folder path in prefix parameter. This will give you a list of blobs starting with that prefix. Once you have that list, you will delete the blobs one by one.

    • You can easily delete a folder including all its contents in Storage Explorer. Otherwise you will need to fetch a list of everything you wish to delete and run those through a loop to delete. For more programmatic solutions please check this thread.
    • Additional information and workaround : Use list_blobs(name_starts_with=folder_name) and delete_blob()

    Complete code:
    blob_service_client = BlobServiceClient.from_connection_string(conn_str=CONN_STR)
    blob_client = blob_service_client.get_container_client(AZURE_BLOBSTORE_CONTAINER)
    [blob_client.delete_blob(blob.name)
    for blob in blob_client.list_blobs(name_starts_with=FOLDER_NAME)]

    • : You cannot delete a non-empty folder in Azure blobs, but you can achieve it if you delete the files inside the sub-folders first. The below work around will start deleting it from the files to the parent folder. from azure.storage.blob import BlockBlobService
      blob_client = BlockBlobService(account_name='', account_key='')
      containername = 'XXX'
      foldername = 'XXX' def delete_folder(containername, foldername):
      folders = [blob.name for blob in blob_client.list_blobs(containername, prefix=foldername)]
      folders.sort(reverse=True, key=len)
      if len(folders) > 0:
      for folder in folders:
      blob_client.delete_blob(containername, folder)
      print("deleted folder",folder)

    Based on the error message you may refer to this Q&A thread which provide multiple option to delete the non empty folders

    Additional information: How to delete a folder within an Azure blob container
    Sample for azure-sdk-for-net

    264816-image.png

    Please let us know if you have any further queries. I’m happy to assist you further.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Please do not forget to 264804-accept-answer.png and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments