Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure storage table delete row by row key

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;
            }
        }
like image 758
Kurkula Avatar asked May 13 '16 03:05

Kurkula


Video Answer


3 Answers

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;
like image 178
Sirar Salih Avatar answered Oct 17 '22 15:10

Sirar Salih


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.

like image 11
Gaurav Mantri Avatar answered Oct 17 '22 20:10

Gaurav Mantri


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;
    }
}    
like image 9
snow_FFFFFF Avatar answered Oct 17 '22 21:10

snow_FFFFFF