All Products
Search
Document Center

Tablestore:Delete data

Last Updated:Aug 29, 2024

Tablestore provides the DeleteRow operation that allows you to delete a single row of data and the BatchWriteRow operation that allows you to delete multiple rows of data at a time.

Usage notes

Deleted data cannot be restored. Proceed with caution.

Prerequisites

Delete a single row of data

You can call the DeleteRow operation to delete a single row of data. If the row that you want to delete 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 data table.

primaryKey

The primary key of the row.

Note

The number and types of primary key columns that you specify must be the same as the actual number and types of primary key columns in the table.

condition

The condition that must be met to perform the operation. You can specify a row existence condition or a condition based on column values. For more information, see Perform conditional updates.

Examples

The following sample code provides an example on how to delete a specific row of data from a table:

// Set the values of the primary key columns of the row that you want to delete to 0 and "abc". 
var primaryKey = new PrimaryKey();
primaryKey.Add("pk0", new ColumnValue(0));
primaryKey.Add("pk1", new ColumnValue("abc"));

try
{
    // Construct a request. RowExistenceExpectation.EXPECT_EXIST indicates that the delete operation is performed only when the specified row exists. 
    var deleteRowRequest = new DeleteRowRequest("SampleTable", new Condition(RowExistenceExpectation.EXPECT_EXIST), primaryKey);

    // Call the DeleteRow operation to delete data. 
    otsClient.DeleteRow(deleteRowRequest);

    // If no exception is returned, the operation is successful. 
    Console.WriteLine("Delete table succeeded.");
}
catch (Exception ex)
{
    // If an exception is returned, the operation fails. Handle the exception. 
    Console.WriteLine("Delete table failed, exception:{0}", ex.Message);
}            

Delete multiple rows of data in a batch

  1. Select a method to query the primary key information about the data you want to delete.

  2. Call the BatchWriteRow operation to delete multiple rows of data at the same time based on the primary key information about the rows. For more information, see Write multiple rows of data in a batch.

References

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