Tablestore provides the DeleteRow operation to delete a single row and the BatchWriteRow operation to delete multiple rows at a time.
Deleted data cannot be restored. Proceed with caution.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created and data is written to it. For more information, see Create data tables and Write data.
Delete a single row
Call the DeleteRow operation to delete a single row of data. If the specified 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 data 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. You can 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 a table named SampleTable. The row is identified by two primary key columns: pk0 (integer) and pk1 (string). The condition RowExistenceExpectation.EXPECT_EXIST ensures the delete operation only proceeds if the row already 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, first retrieve the primary keys of the rows you want to delete, then use BatchWriteRow to delete them in a single batch.
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 data table has a search index, use the search index to query matching rows and obtain their primary keys. For more information, see Basic query.
Delete all rows: If you want to delete all data from a table, we recommend that you delete the table and create a new one with the same configuration. You can also 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. However, a full table scan consumes a large amount of computing resources. Proceed with caution.
Step 2: Batch delete
Call the BatchWriteRow operation to delete the rows identified in Step 1. For more information, see Write multiple rows of data at the same time.
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.