Azure storage table delete row by row key
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;
}
}