Delete Entities from Azure Storage Tables
Learn how to delete entities from an Azure Storage table using various methods.
Introduction
Deleting entities from Azure Table Storage is a common operation. You can delete a single entity or multiple entities efficiently. The primary methods involve using the Azure SDKs for your preferred language or directly interacting with the Azure Storage REST API.
When deleting an entity, you must provide its partition key and row key. For batch deletions, you can group multiple delete operations into a single request, which is more efficient than sending individual delete requests.
Deleting Single Entities
To delete a single entity, you need to know its partition key and row key. This is typically done using a DeleteEntity operation.
Deleting Multiple Entities
For deleting multiple entities, Azure Table Storage supports batch operations. A batch operation allows you to group multiple requests (including deletions) into a single HTTP request. This reduces network latency and improves performance. However, batch operations are restricted to entities within the same partition.
Batch Delete Considerations:
- All entities in a batch operation must belong to the same partition.
- Batch operations have a size limit.
- The
PartitionKeymust be the same for all entities in a batch.
SDK Examples
The following examples demonstrate how to delete entities using the Azure SDK for .NET and Python.
Delete a Single Entity (.NET)
This example shows how to delete a single entity using the Azure.Data.Tables SDK.
using Azure;
using Azure.Data.Tables;
// Replace with your actual connection string and table name
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "MyTable";
string partitionKey = "partition1";
string rowKey = "row1";
TableClient tableClient = new TableClient(connectionString, tableName);
// Delete the entity
try
{
tableClient.DeleteEntity(partitionKey, rowKey);
Console.WriteLine($"Entity with PartitionKey='{partitionKey}' and RowKey='{rowKey}' deleted successfully.");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Error deleting entity: {ex.Message}");
}
Delete Multiple Entities in a Batch (.NET)
This example demonstrates deleting multiple entities within the same partition using a batch operation.
using Azure;
using Azure.Data.Tables;
using System.Collections.Generic;
// Replace with your actual connection string and table name
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "MyTable";
string partitionKey = "partition1";
TableClient tableClient = new TableClient(connectionString, tableName);
// Entities to delete
var entitiesToDelete = new List<TableTransactionAction>
{
new TableTransactionAction(TableTransactionActionType.Delete, new { PartitionKey = partitionKey, RowKey = "row1" }),
new TableTransactionAction(TableTransactionActionType.Delete, new { PartitionKey = partitionKey, RowKey = "row2" }),
new TableTransactionAction(TableTransactionActionType.Delete, new { PartitionKey = partitionKey, RowKey = "row3" })
};
try
{
Response<IReadOnlyList<Response>> response = await tableClient.SubmitTransactionAsync(entitiesToDelete);
Console.WriteLine("Batch delete operation submitted successfully.");
// You can further inspect 'response' for details on each operation
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Error submitting batch delete: {ex.Message}");
}
Delete a Single Entity (Python)
This example shows how to delete a single entity using the Azure Tables SDK for Python.
from azure.data.tables import TableServiceClient
# Replace with your actual connection string and table name
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_name = "MyTable"
partition_key = "partition1"
row_key = "row1"
table_service_client = TableServiceClient.from_connection_string(connection_string)
table_client = table_service_client.get_table_client(table_name)
try:
table_client.delete_entity(partition_key, row_key)
print(f"Entity with PartitionKey='{partition_key}' and RowKey='{row_key}' deleted successfully.")
except Exception as e:
print(f"Error deleting entity: {e}")
Delete Multiple Entities in a Batch (Python)
This example demonstrates deleting multiple entities within the same partition using a batch operation.
from azure.data.tables import TableServiceClient, UpdateMode
# Replace with your actual connection string and table name
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_name = "MyTable"
partition_key = "partition1"
table_service_client = TableServiceClient.from_connection_string(connection_string)
table_client = table_service_client.get_table_client(table_name)
# Entities to delete (using partition_key and row_key)
entities_to_delete = [
{"PartitionKey": partition_key, "RowKey": "row1"},
{"PartitionKey": partition_key, "RowKey": "row2"},
{"PartitionKey": partition_key, "RowKey": "row3"},
]
try:
# Create a list of delete operations
batch_operations = [
table_client.get_entity_delete_operation(entity["PartitionKey"], entity["RowKey"])
for entity in entities_to_delete
]
# Submit the batch
results = table_client.submit_batch(batch_operations)
print("Batch delete operation submitted successfully.")
# The 'results' variable will contain details about the outcome of each operation in the batch
except Exception as e:
print(f"Error submitting batch delete: {e}")
REST API
You can also delete entities using the Azure Storage REST API. This involves making HTTP requests directly to the Table Storage endpoint.
Deleting a Single Entity (REST API)
To delete a single entity, send an HTTP DELETE request to the entity's URI. You must include the x-ms-date and Authorization headers.
Request URI:
/TableServices({accountName})/
/ tableName (PartitionKey='partitionKey', RowKey='rowKey')
HTTP Method:
DELETE
Headers:
x-ms-version: 2019-02-02 x-ms-date: <GMT date and time> Authorization: SharedKey accountName:signature
Authorization header must be computed using your storage account's access key and the request details. Refer to Azure Storage documentation for signature calculation.
Deleting Multiple Entities in a Batch (REST API)
For batch operations, you send a POST request to the $batch endpoint. The request body contains a multipart MIME message with individual operations.
Request URI:
/TableServices({accountName})/ tableName/$batch
HTTP Method:
POST
Headers:
x-ms-version: 2019-02-02 x-ms-date: <GMT date and time> Content-Type: multipart/mixed; boundary="batch_boundary" Authorization: SharedKey accountName:signature
Request Body Example (for deleting entities):
--batch_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /TableServices({accountName})/tableName(PartitionKey='partition1',RowKey='row1') HTTP/1.1
If-Match *
x-ms-date: <GMT date and time>
--batch_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /TableServices({accountName})/tableName(PartitionKey='partition1',RowKey='row2') HTTP/1.1
If-Match *
x-ms-date: <GMT date and time>
--batch_boundary--
Note the use of If-Match * for optimistic concurrency control, which is often recommended for batch operations.