Reputation: 12862
I'm looking for an efficient way to delete a list of blobs from my storage account. There will be a "high number" of blobs to delete which are distributed throughout "many" containers.
Does the Azure Storage client library offer any mechanism to delete a List<BlobId>
from my storage account? Or am I stuck with iterating over each blob, figuring out its container, and deleting individually?
Upvotes: 2
Views: 2245
Reputation: 136196
This answer is now obsolete. Please see answer by Ester Kaufment below. You can delete blobs in batch now.
Does the Azure Storage client library offer any mechanism to delete a List from my storage account?
Sadly, No. Azure Storage Client library simply offers you a Delete Blob
functionality which will delete a single blob at a time.
Or am I stuck with iterating over each blob, figuring out its container, and deleting individually?
You will need to delete each blob individually. However, if you have the URL of the blob that needs to be deleted, then you don't need to figure out the container. Using the blob's URL and storage credentials, you can create an instance of CloudBlob
object and then call DeleteIfExists
or DeleteIfExistsAsync
method to delete the blob. Something like:
var cred = new StorageCredentials(accountName, accountKey);
var blob = new CloudBlob(new Uri("https://myaccount.blob.core.windows.net/mycontainer/myblob.png"), cred);
blob.DeleteIfExists();
Upvotes: 3
Reputation: 868
The accepted answer i no longer correct. Now you can use a new library that Azure provides, that called: Azure Storage Blobs Batch client library for .NET
There is a library for Java as well, of course.
Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. This library allows you to batch multiple Azure Blob Storage operations in a single request.
docs.microsoft/azure/storage.blobs.batch-readme
Code Example:
// Get a connection string to our Azure Storage account.
string connectionString = "<connection_string>";
string containerName = "sample-container";
// Get a reference to a container named "sample-container" and then create it
BlobServiceClient service = new BlobServiceClient(connectionString);
BlobContainerClient container = service.GetBlobContainerClient(containerName);
container.Create();
// Create a blob named "valid"
BlobClient valid = container.GetBlobClient("valid");
valid.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Valid!")));
// Get a reference to a blob named "invalid", but never create it
BlobClient invalid = container.GetBlobClient("invalid");
// Delete both blobs at the same time
BlobBatchClient batch = service.GetBlobBatchClient();
try
{
batch.DeleteBlobs(new Uri[] { valid.Uri, invalid.Uri });
}
catch (AggregateException)
{
// An aggregate exception is thrown for all the individual failures
// Check ex.InnerExceptions for RequestFailedException instances
}
Upvotes: 3