Delete folder(s) inside Azure Blob storage container
Asked Answered
R

3

9

I have a container named "pictures", and have some folders named "Folder1", "Folder2" inside of it. So files of my blob will be addressed like this "http://optimus.blob.core.windows.net/pictures/Folder1/IMG123.png". Using the below C# code to delete the files inside folders,

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(*AzureConnectionString*);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer container = blobClient.GetContainerReference("pictures");

var blobs = container.ListBlobs("Folder1", true);
foreach (var blob in blobs)
{
  container.GetBlockBlobReference(((CloudBlockBlob)blob).Name).DeleteIfExists();
}

after deleting all those files in "Folder1" it will be empty. I m trying to delete the empty folder, but can't get a way to do it. Is it possible to delete the folder(s)? Any help will be much appreciated. Thanks in advance.

Regression answered 29/5, 2015 at 7:14 Comment(0)
W
4

Under any blob container, there is no real folder or directory exists. They are virtual directories to manage the folder structure for the blobs under container and if all the blobs with any virtual directory or folder are deleted then no such folder exists. It is all logical representation of folder structure and you can ignore the folders under any container.

But when it comes to container, you need to manually delete the container as well after deleting its blobs, if you want to clean up the whole container.

Wenz answered 2/6, 2015 at 8:56 Comment(0)
P
6

For Azure Blob Storage, there isn't really a 'folder' object. 'Folders' are actually just blob name prefixes - the '/' character being used to separate 'folders' is a convention in blob naming, not a requirement. So, you don't have to worry about it - as long as there are no blobs with the "Folder1" prefix, then you're fine.

Photocell answered 29/5, 2015 at 19:28 Comment(0)
W
4

Under any blob container, there is no real folder or directory exists. They are virtual directories to manage the folder structure for the blobs under container and if all the blobs with any virtual directory or folder are deleted then no such folder exists. It is all logical representation of folder structure and you can ignore the folders under any container.

But when it comes to container, you need to manually delete the container as well after deleting its blobs, if you want to clean up the whole container.

Wenz answered 2/6, 2015 at 8:56 Comment(0)
C
0

Try using the Azure Blob storage client library v12 for .NET.

            BlobBatchClient batch = service.GetBlobBatchClient();
            await batch.DeleteBlobsAsync(new Uri[] { valid.Uri, invalid.Uri });

Sample avaialble here https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs.Batch/samples/Sample03b_BatchingAsync.cs

Conidium answered 11/12, 2020 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.