I asked Google’s Bard to give me a C# snippet that deletes blobs from Azure Blob Storage
While the Bard FAQ states that “Bard can’t help you with coding just yet,” I thought I’d ask it for a C# snippet and see what happens.
The Prompt
This was my prompt to Bard:
Give me a C# snippet that deletes blobs from Azure Blob Storage
The Response
This was Bard’s primary/first draft response (more on that later).
Sure, here is a C# snippet that deletes blobs from Azure blob storage:
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
namespace DeleteBlobs
{
class Program
{
static void Main(string[] args)
{
// Create a storage client object.
var storageAccount = new StorageAccount(
"myAccountName",
"myAccountKey");
// Create a blob client object.
var blobClient = storageAccount.CreateBlobClient("myContainer");
// Get the list of blobs in the container.
var blobList = await blobClient.ListBlobsAsync();
// Loop through the list of blobs and delete each one.
foreach (var blob in blobList)
{
// Delete the blob.
await blobClient.DeleteAsync(blob.Name);
}
// Print a message to indicate that the blobs have been deleted.
Console.WriteLine("All blobs have been deleted.");
}
}
}
This code snippet will delete all blobs from the container “myContainer” in the storage account “myAccountName”. To use this code snippet, you will need to replace the following values:
myAccountName
with the name of your Azure storage account.myAccountKey
with the primary or secondary access key for your Azure storage account.myContainer
with the name of the container that contains the blobs you want to delete.You can find these values in the Azure portal or by using the Azure Storage Explorer.
At first glance, it looks maybe not too bad, right? I got some code, which looks like it might work, and a nice explanation to wrap it up. But does it work? Let’s find out.
Preparing to Run the Snippet
To get setup, I thought about first hopping into the Microsoft Azure Portal, creating a new storage account, adding a container, and uploading a handful of blobs.
But first I thought I’d fire up Visual Studio to see if this thing builds. So I created a new Console App project, and replaced the entire Program.cs with Bard’s code.
Right away, Resharper complains because we’re using the await operator without being in an async method. That’s an easy fix, so now our Main signature looks like this:
static async Task Main(string[] args)
Then I notice we’re using Microsoft.Azure.Storage, and that’s not great, because Microsoft deprecated that package long ago in favor of Azure.Storage.Blobs. But it’s not a show-stopper, because we can still install the package and keep going.
But now we’ve got another issue, because StorageAccount doesn’t exist. I’m going to assume we really want CloudStorageAccount. You can’t construct one with just an account name and account key, so I’ll fix that up too. Something like this:
var credentials = new StorageCredentials(“myAccountName”, “myAccountKey”);
var storageAccount = new CloudStorageAccount(credentials, true);
Now the next problem is that CloudStorageAccount doesn’t have a method called CreateBlobClient. It does have a method called CreateCloudBlobClient, so we’ll fix that up.
var blobClient = storageAccount.CreateCloudBlobClient();
But now we realize that ListBlobsAsync and DeleteAsync don’t exist. And if I wasn’t questioning this whole exercise before, I’m definitely questioning it now.
But I don’t like to give up, so ListBlobsAsync() becomes ListBlobs(“”), and blobClient.DeleteAsync(blob.Name) becomes blob.Container.DeleteAsync().
We’re clean up all the unnecessary System usings, and finally end up here:
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;
namespace DeleteBlobs
{
class Program
{
static async Task Main(string[] args)
{
// Create a storage client object.
var credentials = new StorageCredentials("myAccountName", "myAccountKey");
var storageAccount = new CloudStorageAccount(credentials, true);
// Create a blob client object.
var blobClient = storageAccount.CreateCloudBlobClient();
// Get the list of blobs in the container.
var blobList = blobClient.ListBlobs("");
// Loop through the list of blobs and delete each one.
foreach (var blob in blobList)
{
// Delete the blob.
await blob.Container.DeleteAsync();
}
// Print a message to indicate that the blobs have been deleted.
Console.WriteLine("All blobs have been deleted.");
}
}
}
Now I’ll hop into the Microsoft Azure Portal, create a new storage account, add a container, and upload a handful of blobs.
Running The Snippet
We replace our myAccountName and myAccountKey, hit F5, and….
Check back next week to see what happened!
Just kidding.
But it didn’t go well:
Microsoft.Azure.Storage.StorageException: ‘The specified container does not exist.’
This makes sense since we haven’t specified a container anywhere, and you can’t really operate on blobs at the account level (please correct me if I’m wrong here). So we’ll grab a container reference and use that to list our blobs, for our final version:
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;
namespace DeleteBlobs
{
class Program
{
static async Task Main(string[] args)
{
// Create a storage client object.
var credentials = new StorageCredentials("mediumplayground", "XeQNK84kmNT+zmwRTo8zsEksf573FsFSrMNg2AnLYjJn5pF6xeVDVrklEbw4wIG31UwAA/SYnThl+AStTLASbw==");
var storageAccount = new CloudStorageAccount(credentials, true);
// Create a blob client object.
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mytestcontainer");
// Get the list of blobs in the container.
var blobList = container.ListBlobs("");
// Loop through the list of blobs and delete each one.
foreach (var blob in blobList)
{
// Delete the blob.
await blob.Container.DeleteAsync();
}
// Print a message to indicate that the blobs have been deleted.
Console.WriteLine("All blobs have been deleted.");
}
}
}
F5 that thing, and BAM:
All blobs have been deleted.
Additional Responses
When Bard responds, it provides the option to “View other drafts,” giving you the chance to see two other drafts that Bard cooked up for you.
Draft 2
Draft 2 took the same logical approach as Draft 1, but with some notable structural differences:
- Less using statements.
- No namespace declaration.
- Class is named DeleteBlobs instead of Program, though still uses a static Main(string[] args) method.
- Opted to use non-async methods.
See for yourself:
Sure, here is a C# snippet that deletes blobs from Azure blob storage:
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blobs;
public class DeleteBlobs
{
public static void Main(string[] args)
{
// Create a storage account client object.
var storageAccount = new StorageAccount(
"myaccountname",
"myaccountkey");
// Create a blob client object.
var blobClient = storageAccount.CreateBlobClient("mycontainer");
// List the blobs in the container.
var blobList = blobClient.ListBlobs();
// Loop through the blobs and delete them.
foreach (var blob in blobList)
{
blobClient.DeleteBlob(blob.Name);
}
}
}
This snippet will delete all blobs from the container named "mycontainer". You can modify it to delete specific blobs by adding a condition to the loop. For example, to delete only blobs that were created after a certain date, you could add the following condition:
if (blob.CreationTime > DateTime.UtcNow - TimeSpan.FromDays(1))
{
blobClient.DeleteBlob(blob.Name);
}
I really like that it followed-up with a snippet demonstrating how to tweak the example to delete blobs created within the last 24 hours, even if we know from our experience with Draft 1 that this code does not work.
Draft 3
Draft 3 was interesting because it uses BlobServiceClient from the preferred Azure.Storage.Blobs package, but doesn’t include the appropriate using statement, and generally tries to blend the new with the old. Take a look:
Sure, here is a C# snippet that deletes blobs from Azure blob Storage:
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blobs;
namespace BlobDeleter
{
class Program
{
static async Task Main(string[] args)
{
// Create a new BlobServiceClient object.
var storageAccount = new StorageAccount("myaccountname", "myaccountkey");
var blobServiceClient = new BlobServiceClient(storageAccount);
// Get the container reference.
var container = blobServiceClient.GetContainerReference("mycontainer");
// List the blobs in the container.
var blobList = await container.ListBlobsAsync();
// Loop through the blobs and delete them.
foreach (var blob in blobList)
{
await blob.DeleteAsync();
}
Console.WriteLine("All blobs deleted.");
}
}
}
This snippet will delete all blobs in the container named
mycontainer
. You can modify the code to delete specific blobs or blobs that match a certain criteria.
Conclusion
I think we had some fun with Google Bard, even if the code didn’t build and was generally pretty flawed. Of course, we can’t really fault it — Google told us that Bard wasn’t ready for coding. It might be interesting to follow-up in a couple of weeks and see whether we get a better response.
Google Bard availability is currently subject to a waitlist, so head over and sign up if you’re interested.