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;
}
}
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;
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With