Tablestore provides the DeleteRow operation that you can call to delete a single row of data and the BatchWriteRow operation that you can call to delete multiple rows of data at the same time.
Usage notes
The data that you delete cannot be restored. Proceed with caution.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize a client.
A data table is created, and data is written to the data 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 data table remains unchanged.
Parameters
Parameter | Description |
tableName | The name of the data table. |
primaryKey | The primary key of the row. The value of this parameter consists of the name, type, and value of each primary key column. Important 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 configure a row existence condition or a condition based on column values. For more information, see Perform conditional updates. |
Examples
Delete a row of data
The following sample code provides an example on how to delete a row of data from a table:
private static void deleteRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the data table.
RowDeleteChange rowDeleteChange = new RowDeleteChange("<TABLE_NAME>", primaryKey);
client.deleteRow(new DeleteRowRequest(rowDeleteChange));
}
Specify conditions to delete a row of data
The following sample code provides an example on how to delete a row of data from a data table when the row exists and the value of the Col0 column in the row is greater than 100:
private static void deleteRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the data table.
RowDeleteChange rowDeleteChange = new RowDeleteChange("<TABLE_NAME>", primaryKey);
// Specify conditions for the DeleteRow operation. In this example, a row is deleted only when the row exists and the value of the Col0 column is greater than 100.
Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
condition.setColumnCondition(new SingleColumnValueCondition("Col0",
SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100)));
rowDeleteChange.setCondition(condition);
client.deleteRow(new DeleteRowRequest(rowDeleteChange));
}
Delete multiple rows of data at a time
Before you delete data, select a suitable method based on your business requirements to query the primary key information about the data that you want to delete.
To delete data whose primary key values are within a specific range, call the GetRange operation to query the data and obtain the primary key information about the data. For more information, see Read data whose primary key values are within a specific range.
To delete data that meets specific conditions from a data table for which a search index is created, use the search index to query the data and obtain the primary key information about the data. For more information, see Basic query.
To delete all data from a data table, we recommend that you delete the data table and create a data table that has the same configurations.
You can also call the GetRange operation and set the start primary key to INF_MIN and the end primary key to INF_MAX to scan all data in the table. This way, you can obtain the primary key information about all data in the table. However, this consumes a large amount of computing resources. Proceed with caution.
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 data.
The following sample code provides an example on how to delete a row of data in which the value of the pk primary key column is
pk
from a data table and a row of data in which the value of the pk1 primary key column ispk1
and the value of the pk2 primary key column ispk2
from another data table at the same time:private static void batchWriteRow(SyncClient client) { BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest(); // Construct rowDeleteChange1. PrimaryKeyBuilder pk1Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); pk1Builder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("pk")); // Specify the name of the data table. RowDeleteChange rowDeleteChange1 = new RowDeleteChange("<TABLE_NAME1>", pk1Builder.build()); // Add rowDeleteChange1 to the code of the batch operation. batchWriteRowRequest.addRowChange(rowDeleteChange1); // Construct rowDeleteChange2. PrimaryKeyBuilder pk2Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); pk2Builder.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromString("pk1")); pk2Builder.addPrimaryKeyColumn("pk2", PrimaryKeyValue.fromString("pk2")); // Specify the name of the data table. RowDeleteChange rowDeleteChange2 = new RowDeleteChange("<TABLE_NAME2>", pk2Builder.build()); // Add rowDeleteChange2 to the code of the batch operation. batchWriteRowRequest.addRowChange(rowDeleteChange2); BatchWriteRowResponse response = client.batchWriteRow(batchWriteRowRequest); System.out.println("Whether all operations are successful:" + response.isAllSucceed()); if (!response.isAllSucceed()) { for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) { System.out.println("Failed rows:" + batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey()); System.out.println("Cause of failures:" + rowResult.getError()); } /** * You can use the createRequestForRetry method to construct another request to retry the operations on failed rows. In this example, only the retry request is constructed. * We recommend that you use the custom retry policy in Tablestore SDKs as the retry method. This feature allows you to retry failed rows after batch operations are performed. After you specify the retry policy, you do not need to add retry code to call the operation. */ BatchWriteRowRequest retryRequest = batchWriteRowRequest.createRequestForRetry(response.getFailedRows()); } }
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.