How do i delete a folder(blob) inside an azure container using delete_blob method of blockblobservice?
Asked Answered
C

5

5

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?

Cachexia answered 20/3, 2018 at 14:50 Comment(0)
T
10

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.

Tylertylosis answered 21/3, 2018 at 4:41 Comment(10)
Thank you so much Gaurav ! Your approach helped a lot :)Cachexia
Also Gaurav, how do i determine if a blob is a file or virtual folder? blob.name gives us the name. likewise, is there any parameter which tells us if it's a file or virtual folder?Cachexia
If you look at listing results, I believe the virtual folders are of type "BlobPrefix". That should be a good indicator if the item is a folder or a blob. Otherwise, you can check for the existence of properties. For virtual folders you should not get any properties back.Tylertylosis
One more thing....If you want to list only blobs and not virtual folders inside a container/virtual folder, please specify delimiter as empty string. This will return you a list of blobs only. HTH.Tylertylosis
Gaurav, I'm trying to download the blobs to local file system using the following code : generator = session.list_blobs(container_name,path_to_file+'/',delimiter='/') for blob in generator: bl = os.path.basename(blob.name) session.get_blob_to_path(container_name,bl,bl) only the first blob gets downloaded but i have azure.common.AzureMissingResourceHttpError and it says the specified blob doesn't exist. Due to this, i'm not able to download the rest. could you please suggest on how to get rid of this error?Cachexia
Can you please ask a separate question for this? When you do, please include the complete code you've written and the detailed error message you're getting.Tylertylosis
Done. the question is here : #49411182Cachexia
Gaurav, could you please take a look at the new question? I'm stuck with these exceptions.Cachexia
I did take a look at that question yesterday only. Since I am not too familiar with Python, can you please edit your question and include various values (debug information) in your question. That way it will be easier to identify where things are going wrong. I think it is a minor logic issue and nothing big. HTH.Tylertylosis
@GauravMantri Deleting individual blobs is not deleting the virutal folders for me. I have written a question for it #76815046Similarity
A
7

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

Alialia answered 20/3, 2018 at 22:57 Comment(7)
Thank you for answering. delete_container() deletes the whole container. But i want to delete only a folder under that container. Is there any way to do that?Cachexia
You should use Delete_blob, using the following syntax: .delete_blob(container_name, 'myblob') I'll add it to the answer.Alialia
Adam, I've been trying the same thing: b.delete_blob('29azurefs','one') and i get the following exception: Client-Request-ID=eb93b964-2cc1-11e8-b3fd-00155dbf7128 Retry policy did not allow for a retry: Server-Timestamp=Wed, 21 Mar 2018 04:40:02 GMT, Server-Request-ID=5e33d75b-701e-0083-3bce-c04e35000000, HTTP status code=404, Exception=The specified blob does not exist.ErrorCode: BlobNotFound<?xml version="1.0" encoding="utf-8"?><Error><Code>BlobNotFound</Code><Message>The specified blob does not exist.RequestId:5e33d75b-701e-0083-3bce-c04e35000000Time:2018-03-21T04:40:03.1598904ZCachexia
Are you able to see the blob when listing it? Can you add additional attributes besides the blob name to the delete_blob?Alialia
As i mentioned, using delete_blob(<container_name>,<blob_name>), i'm able to delete only the files. if i specify a folder name for blob name, then it's returning an exception.Cachexia
@Cachexia You can't delete a folder. Please see my answer for the explanation.Tylertylosis
Not working :( Getting index error when it's unable to find the blobs.Incorruption
G
4

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)
Goolsby answered 26/4, 2018 at 9:51 Comment(1)
.list_blobs(container_name, prefix=foldername) would accelerate request dramaticlyCouchman
C
4

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)
Carvelbuilt answered 2/7, 2021 at 4:49 Comment(0)
M
3

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)
Mutz answered 27/4, 2020 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.