Delete an Entity from Azure Storage Tables

This guide demonstrates how to delete a specific entity from an Azure Storage table using various SDKs and REST API.

Note: Deleting an entity is a permanent operation. Ensure you have backed up any critical data before proceeding.

Using Azure CLI

You can delete an entity using the az storage entity delete command.

Command:
az storage entity delete --table-name MyTable \
    --partition-key PartitionKey1 \
    --row-key RowKey1 \
    --account-name mystorageaccount \
    --account-key 'YOUR_STORAGE_ACCOUNT_KEY'

Replace MyTable, PartitionKey1, RowKey1, mystorageaccount, and YOUR_STORAGE_ACCOUNT_KEY with your specific values.

Using Azure PowerShell

Use the Remove-AzStorageTableEntity cmdlet to delete an entity.

Command:
Remove-AzStorageTableEntity -TableName MyTable `
    -PartitionKey "PartitionKey1" `
    -RowKey "RowKey1" `
    -Context $ctx

Ensure you have set up your storage account context ($ctx) beforehand.

Using .NET SDK

The Azure SDK for .NET provides methods to interact with Azure Storage Tables.

C# Code:
using Azure.Data.Tables;

// ...

var connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
var tableName = "MyTable";
var partitionKey = "PartitionKey1";
var rowKey = "RowKey1";

var client = new TableClient(connectionString, tableName);

try
{
    client.DeleteEntity(partitionKey, rowKey);
    Console.WriteLine($"Entity with PartitionKey='{partitionKey}' and RowKey='{rowKey}' deleted successfully.");
}
catch (Exception ex)
{
    Console.WriteLine($"Error deleting entity: {ex.Message}");
}

Make sure to install the Azure.Data.Tables NuGet package.

Using Python SDK

The Azure SDK for Python simplifies table operations.

Python Code:
from azure.data.tables import TableClient

# ...

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_name = "MyTable"
partition_key = "PartitionKey1"
row_key = "RowKey1"

table_client = TableClient.from_connection_string(connection_string, 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}")

Install the SDK using: pip install azure-data-tables

Using Java SDK

Leverage the Azure SDK for Java for table entity management.

Java Code:
import com.azure.data.tables.TableClient;
import com.azure.data.tables.TableServiceClient;
import com.azure.data.tables.TableServiceClientBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;

// ...

String connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
String tableName = "MyTable";
String partitionKey = "PartitionKey1";
String rowKey = "RowKey1";

TableServiceClient tableServiceClient = new TableServiceClientBuilder()
    .connectionString(connectionString)
    .buildClient();

TableClient tableClient = tableServiceClient.getTableClient(tableName);

try {
    tableClient.deleteEntity(partitionKey, rowKey);
    System.out.println("Entity deleted successfully.");
} catch (Exception e) {
    System.err.println("Error deleting entity: " + e.getMessage());
}

Ensure you have the Azure Tables dependency in your project.

Using REST API

You can also delete entities directly using the Azure Storage Tables REST API.

Request URL:
GET https://<storage-account-name>.table.core.windows.net/<table-name>(PartitionKey='<partition-key>',RowKey='<row-key>')?sv=<sas-token> HTTP/1.1
Request Headers:
Accept: application/json;odata=fullmetadata
DataServiceVersion: 3.0;NetFx
Content-Type: application/json
HTTP Method:
DELETE

You will need to construct the URL with your storage account name, table name, partition key, row key, and a valid Shared Access Signature (SAS) token or use authentication with Azure AD.

View Table REST API Reference
Important: Always use appropriate authentication methods and secure your storage account keys. Consider using Azure AD authentication for better security.