Azure Tips and Tricks Part 86 - Deleting an item from a Azure Storage Table

2 minute read

Azure Community Spotlight Azure is a big space and it’s easy to get lost and stay in the know. In this section, I’d like to call out a member of the community and their expertise, so that you can gain insight on Azure in terms of the corner of the platform that they cover.

Scott Cate works at Microsoft and is part of the Azure Cloud Developer Advocates team that focuses on .NET and Xamarin. Learn more about Scott here or follow him on Twitter .

Intro

Most folks aren’t aware of how powerful the Azure platform really is. As I’ve been presenting topics on Azure, I’ve had many people say, “How did you do that?” So I’ll be documenting my tips and tricks for Azure in these posts.

The Complete List of Azure Tips and Tricks

Available Now!

Deleting an item from a Azure Storage Table

In case you are new to the Azure Storage Tables, we’ve reviewed the following items this week:

Today, we’ll be taking a look at deleting an item through C# code into an Azure Storage Table.

Getting Started

Open the C# Console application that we were working with last week and let’s add a method to:

  • Delete an item based off of the table, RowKey and PartitionKey that we pass in.

Delete an item

In our Program.cs file, we’ll now add in a helper method that passes in a table, RowKey and PartitionKey to identify the message we want to delete.

static void DeleteMessage(CloudTable table, string partitionKey, string rowKey)
{
    TableOperation retrieve = TableOperation.Retrieve<Thanks>(partitionKey, rowKey);

    TableResult result = table.Execute(retrieve);

    var deleteEntity = (Thanks)result.Result;

    TableOperation delete = TableOperation.Delete(deleteEntity);

    table.Execute(delete);
}

In this example, we retrieve the message and then delete the entity.

Putting it all together.

The Main method inside of the Program.cs file, we’ll call our helper method.

static void Main(string[] args)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("StorageConnection"));

    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

    CloudTable table = tableClient.GetTableReference("thankfulfor");

    table.CreateIfNotExists();

    //added these lines
    DeleteMessage(table, "ThanksApp", "I'm thankful for the time with my family");
    //added these lines

    table.Execute(update);
    Console.ReadKey();

}

If we run the program now, then it will delete the message that we specified. As described earlier, you can use the Azure Storage Explorer if you don’t need access to the table through code. If you come back tomorrow, then I’ll show you how to delete this item through code.

Want more Azure Tips and Tricks?

If you’d like to learn more Azure Tips and Tricks, then follow me on twitter or stay tuned to this blog! I’d also love to hear your tips and tricks for working in Azure, just leave a comment below.

Leave a Comment