All Products
Search
Document Center

Tablestore:Delete data

Last Updated:Jun 03, 2026

Tablestore provides the DeleteRow operation to delete a single row and the BatchWriteRow operation to delete multiple rows at a time.

Warning

Deleted data cannot be restored. Proceed with caution.

Prerequisites

Before you begin, ensure that you have:

Delete a single row

Call DeleteRow to delete a single row. If the row does not exist, the table remains unchanged.

API operations

/// <summary>
/// Delete a row of data based on the specified table name and primary key.
/// </summary>
/// <param name="request">Request instance</param>
/// <returns>Response instance</returns>
public DeleteRowResponse DeleteRow(DeleteRowRequest request);

/// <summary>
/// The asynchronous mode of DeleteRow.
/// </summary>
public Task<DeleteRowResponse> DeleteRowAsync(DeleteRowRequest request);

Parameters

Parameter Description
tableName The name of the table.
primaryKey The primary key of the row to delete.
Note

The number and types of primary key columns must match the schema defined when the table was created.

condition The condition that must be met before the row is deleted. Specify a row existence condition or a condition based on column values. For more information, see Perform conditional updates.

Example

The following example deletes a row from SampleTable. The row has two primary key columns: pk0 (integer) and pk1 (string). The condition RowExistenceExpectation.EXPECT_EXIST ensures the operation proceeds only if the row exists.

// Define the primary key of the row to delete.
// In this example, pk0 = 0 and pk1 = "abc".
var primaryKey = new PrimaryKey();
primaryKey.Add("pk0", new ColumnValue(0));
primaryKey.Add("pk1", new ColumnValue("abc"));

try
{
    // Construct the delete request.
    // EXPECT_EXIST means the operation succeeds only if the row exists.
    var deleteRowRequest = new DeleteRowRequest(
        "SampleTable",
        new Condition(RowExistenceExpectation.EXPECT_EXIST),
        primaryKey);

    // Execute the delete operation.
    otsClient.DeleteRow(deleteRowRequest);

    // No exception means the operation succeeded.
    Console.WriteLine("Delete row succeeded.");
}
catch (Exception ex)
{
    // An exception indicates the operation failed.
    Console.WriteLine("Delete row failed, exception:{0}", ex.Message);
}

Delete multiple rows

To delete multiple rows, retrieve the primary keys of the target rows and pass them to BatchWriteRow in a single batch request.

Step 1: Retrieve primary keys

Choose one of the following methods based on your use case:

  • Delete rows within a primary key range: Call GetRange to query rows in a specified range and obtain their primary keys. For more information, see Read data whose primary key values are within a specific range.

  • Delete rows that match specific conditions: If the table has a search index, use it to query matching rows and obtain their primary keys. For more information, see Basic query.

  • Delete all rows: To delete all data from a table, the recommended approach is to delete the table and create a new one with the same configuration. Alternatively, call GetRange with the start primary key set to INF_MIN and the end primary key set to INF_MAX to scan all rows and retrieve their primary keys. A full table scan consumes significant computing resources. Proceed with caution.

Step 2: Batch delete

Call BatchWriteRow to delete the rows identified in Step 1. For more information, see Write multiple rows of data at the same time.

After the batch completes, check the response for unprocessed rows. Rows may remain unprocessed if a throughput limit is reached or a transient error occurs. Retry unprocessed rows until all deletes succeed.

References

Time to live (TTL) specifies the retention period of data. Configure TTL on a table to automatically delete expired data. For more information, see Data versions and TTL.