using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; using System.Threading.Tasks; //Retrieve connection string from an environment variable var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); //Create a CloudStorageAccount object from the connection string var storageAccount = CloudStorageAccount.Parse(connectionString); //Create a CloudBlobClient object from the storage account var blobClient = storageAccount.CreateCloudBlobClient(); //Get a reference to a container var container = blobClient.GetContainerReference("mycontainer"); //Delete the container if it exists var deleted = await container.DeleteIfExistsAsync(); if (deleted) { Console.WriteLine("Container deleted successfully"); } else { Console.WriteLine("Container does not exist"); }
using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; using System.Threading.Tasks; //Retrieve connection string from an environment variable var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); //Create a CloudStorageAccount object from the connection string var storageAccount = CloudStorageAccount.Parse(connectionString); //Create a CloudBlobClient object from the storage account var blobClient = storageAccount.CreateCloudBlobClient(); //Get a reference to a container var container = blobClient.GetContainerReference("mycontainer"); //Check if the container exists before deleting it if (await container.ExistsAsync()) { await container.DeleteAsync(); Console.WriteLine("Container deleted successfully"); } else { Console.WriteLine("Container does not exist"); }In both examples, we are using the Microsoft.WindowsAzure.Storage.Blob package library to delete a container if it exists. The containers are retrieved using a CloudBlobClient object, which is created from a CloudStorageAccount object that is created from the connection string. We are also using the async/await syntax to execute the asynchronous DeleteIfExistsAsync method.