Microsoft Azure – Storage – Part 5 – Delete blobs with a .NET application

In the prior article of the Microsoft Azure storage series we have downloaded a file (blob) from the container. In this part we will add a method to delete a blob in the Azure storage container.

Delete a blob  in the storage container

Add the DeleteBlob method to the Azure_Helper

  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add the following method to the class
/// <summary>
/// Deletes a blob in the Azure storage
/// </summary>
/// <param name="sContainerPath"></param>
/// <param name="sBlobName"></param>
public void DeleteBlob(string sContainerPath, string sBlobName) {

    // Create the blob client.
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    // Retrieve reference to a blob named "myblob.txt".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(sBlobName);

    // Delete the blob.
    blockBlob.Delete();
}

Call the Azure_Helper from the Console Application

  • Open the Program.cs file in the Azure_ConsoleApplication
  • Add the following to the end of the Main method
// Delete the blob from the Azure storage
azureHelper.DeleteBlob(sContainerPath, sBlobName);

When you are testing the application the first time you can place a break point to the line that calls the DeleteBlob method and use the Azure manager  at https://manage.windowsazure.com  to follow the creation and deletion of the blob in the Azure storage container.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *