9
votes

I am trying to delete row from azure storage filter by only rowkey value. But I dont see any overload for delete operation where we can filter with only rowkey. Is there any alternative option to delete row from azure storage table for records with specific rowkey?

RemoveEntityByRowKey('123456');
public static void RemoveEntityByRowKey(string myRowKey)
        {
            try
            {
                CloudTable table = _tableClient.GetTableReference("MyAzureTable"); 
                       TableOperation delteOperation = TableOperation.Delete(myRowKey);
                table.Execute(delteOperation);
            }
            catch (Exception ex)
            {
                LogError.LogErrorToAzure(ex);
                throw;
            }
        }
6

6 Answers

11
votes

In order to delete an entity, you would need both PartitionKey and RowKey (Delete Entity REST API). So what you would need to do is first fetch the entity with matching RowKey. Once you have fetched this entity, you should be able to call TableOperation.Delete as mentioned in the answers.

However, fetching entity by RowKey is not recommended because it will do a Full Table Scan. It may not be a problem if your table size is small but would be an issue where your table contains large number of entities. Furthermore, a RowKey is unique in a Partition i.e. in a table there can be only one entity with a PartitionKey/RowKey combination. In other words, you can potentially have entities with same RowKey in different Partitions. So when you fetch entities by RowKey only, you may get more than one entity back. You need to ensure that you're deleting correct entity.

6
votes

If you know the PartitionKey as well as the RowKey, you don't need to retrieve the entire entity to delete it. You could adapt your code as follows:

public static void RemoveEntityByRowKey(string myRowKey)
{
    try
    {
        var entity = new YourEntity 
        {
            PartitionKey = "partition",
            RowKey = myRowKey,
            ETag = "*"
        }

        CloudTable table = _tableClient.GetTableReference("MyAzureTable"); 
        TableOperation delteOperation = TableOperation.Delete(entity);
        table.Execute(delteOperation);
    }
    catch (Exception ex)
    {
        LogError.LogErrorToAzure(ex);
        throw;
    }
}    
4
votes

If you're targeting .NET Core, you'll need to use the ExecuteQuerySegmentedAsync method to execute the filter condition query. ExecuteQuery is deprecated.

var cloudTableClient = _cloudStorageAccount.CreateCloudTableClient();
var myTable = cloudTableClient.GetTableReference("MyTable");
var query = new TableQuery<MyEntity>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "myRowKey"));
var segment = await myTable.ExecuteQuerySegmentedAsync(query, null);
var myEntities = segment.Results;
3
votes

By row do you mean a record ?

TableOperation.Delete accepts a Table Entity. See here: https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableoperation.delete.aspx

In order to delete that entity, you must first retrieve it by specifying its Partition key and/or Row key.

Look into TableQuery class here https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tablequery_methods.aspx

Once you retrieve it, pass it to Delete method.

2
votes

There is no method

TableOperation.Delete(String rowKey),

only method

public static TableOperation delete(final TableEntity entity)

in TableOperation. For details, see Get started with Azure Table storage using .NET

0
votes

With reference to Franks pointers, I am posting answer so that it would be useful to others who face the similar issue.

RemoveEntityByRowKey('123456');
public static void RemoveEntityByRowKey(string myRowKey)
        {
            try
            {
                CloudTable table = _tableClient.GetTableReference("MyAzureTable"); 

   TableQuery<myEntity> query = new TableQuery<myEntity>()
                   .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, myRowKey));

                foreach (var item in table.ExecuteQuery(query))
                {
                    var oper = TableOperation.Delete(item);
                    table.Execute(oper);                    
                } 
            }
            catch (Exception ex)
            {
                LogErrorToAzure(ex);
                throw;
            }
        }