Tablestore provides the DeleteRow operation to allow you to delete a single row of data and the BatchWriteRow operation to allow you to delete multiple rows of data in a batch.
Important The data that you delete cannot be restored. Proceed with caution.
Prerequisites
- The OTSClient instance is initialized. For more information, see Initialization.
- A data table is created. Data is written to the table.
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 the primary key columns that you specify must be the same as the actual number and types of primary key columns in the data table. |
condition | The condition that you want to specify to perform the DeleteRow operation. You can specify a row existence condition or a condition based on column values. For more information, see Configure conditional update. |
Examples
The following code provides an example on how to delete a row of data:
// 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 the operation is successful, no exception is returned.
Console.Writeline("Delete table succeeded.");
}
catch (Exception ex)
{
// If the operation fails, an exception is returned. Handle the exception.
Console.WriteLine("Delete table failed, exception:{0}", ex.Message);
}
To view the detailed sample code, visit DeleteRow@GitHub.
Delete multiple rows of data in a batch
- Select a method to query the primary key information about the rows that you want to delete.
- To query the data in the specified primary key range and obtain the primary key information about the data, call the GetRange operation. Then, you can delete the data. For more information, see Read data whose primary key is in a specified range.
- To delete the data that meets the specified conditions, use search indexes to query the data. Then, obtain the primary key information about the data. For more information, see Create search indexes and Use Tablestore SDKs.
- To delete multiple rows of data in a batch based on the primary key information, call the BatchWriteRow operation. For more information, see Write multiple rows of data in a batch.