2
votes

delete_blob() seems to delete only the files inside the container and from folders and subfolders inside the container. But i'm seeing below error in python while trying to delete a folder from container.

Client-Request-ID=7950669c-2c4a-11e8-88e7-00155dbf7128 Retry policy did not allow for a retry: Server-Timestamp=Tue, 20 Mar 2018 14:25:00 GMT, Server-Request-ID=54d1a5d6-b01e-007b-5e57-c08528000000, HTTP status code=404, Exception=The specified blob does not exist.ErrorCode: BlobNotFoundBlobNotFoundThe specified blob does not exist.RequestId:54d1a5d6-b01e-007b-5e57-c08528000000Time:2018-03-20T14:25:01.2130063Z.

azure.common.AzureMissingResourceHttpError: The specified blob does not exist.ErrorCode: BlobNotFound BlobNotFoundThe specified blob does not exist. RequestId:54d1a5d6-b01e-007b-5e57-c08528000000 Time:2018-03-20T14:25:01.2130063Z

Could anyone please help here?

5

5 Answers

5
votes

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.

6
votes

There are two things to understand from the process, you could delete specific files,folders,images...(blobs) using delete_blob , But if you want to delete containers, you have to use the delete_container which will delete all blobs within, here's a sample that i created which deletes blobs inside a path/virtual folder:

from azure.storage.blob import BlockBlobService

block_blob_service = BlockBlobService(account_name='yraccountname', account_key='accountkey')
print("Retreiving blobs in specified container...")
blob_list=[]
container="containername"
def list_blobs(container):
        try:

                global blob_list
                content = block_blob_service.list_blobs(container)
                print("******Blobs currently in the container:**********")
                for blob in content:
                        blob_list.append(blob.name)
                        print(blob.name)
        except:
                print("The specified container does not exist, Please check the container name or if it exists.")
list_blobs(container)
print("The list() is:")
print(blob_list)
print("Delete this blob: ",blob_list[1])
#DELETE A SPECIFIC BLOB FROM THE CONTAINER
block_blob_service.delete_blob(container,blob_list[1],snapshot=None)
list_blobs(container)

Please refer to the code in my repo with explanation in Readme section, as well as new storage scripts:https://github.com/adamsmith0016/Azure-storage

3
votes

For others searching for the solution in python. This worked for me.

First make a variable that stores all the files in the folder that you want to remove.

Then for every file in that folder, remove the file by stating the name of the container, and then the actual foldername.name .

By removing all the files in a folder, the folders is deleted in azure.

def delete_folder(self, containername, foldername):
    folders = [blob for blob in blob_service.block_blob_service.list_blobs(containername) if blob.name.startswith(foldername)]
    if len(folders) > 0:
        for folder in folders:
            blob_service.block_blob_service.delete_blob(containername, foldername.name)
            print("deleted folder",folder name)
3
votes

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)
1
votes

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)

for blob in blob_client.list_blobs(name_starts_with=FOLDER_NAME):
    blob_client.delete_blob(blob.name)