Note
The basic unit of a table is a row, which consists of primary key columns and attribute columns. Primary key columns are required, and their names and types must be the same for all rows. Attribute columns are optional, and the attributes can be different for each row. For more information, see Wide table model.
Usage notes
-
When deleting data, the number and types of the primary key columns you specify must match the table's primary key schema.
-
Deleted data cannot be recovered. Proceed with caution.
-
When the table contains a large volume of data, a deletion operation (especially batch deletion or asynchronous deletion involving large amounts of data) may take a long time to complete. For example, it is normal for the operation to take more than 48 hours. We recommend that you wait patiently and monitor the task status without resubmitting the deletion request.
Delete a single row
Call the DeleteRow operation to delete a single row. If the specified row does not exist, the operation has no effect.
When you delete a row, you can use a conditional update to delete the row only if it meets specific conditions. For more information, see conditional update.
Delete multiple rows
After obtaining the primary keys of the rows to delete, call the BatchWriteRow operation to delete them in a batch.
Note
To automatically delete data older than a specific time, you can use the time to live (TTL) feature. For more information, see time to live (TTL).
When deleting rows in a batch, you have the following options:
-
Delete rows from multiple tables in a single request.
-
Use a conditional update to delete rows only if they meet specific conditions. For more information, see conditional update.
Methods
Console
You can use the Tablestore console to delete a single row or multiple rows.
-
Log on to the Tablestore console.
-
On the Overview page, find the instance you want to manage and click Manage Instance in the Actions column.
-
On the Instance Details tab, click a table name in the Tables section.
-
On the Query Data tab, select one or more rows to delete, and then click Delete at the bottom of the page.
-
In the Delete dialog box, click OK.
Tablestore CLI
You can run the delete command in the Tablestore CLI to delete a row.
The following example deletes a row where the first primary key column is "86" and the second is 6771.
delete --pk '["86", 6771]'
SDKs
You can use the Java SDK, Go SDK, Python SDK, Node.js SDK, .NET SDK, or PHP SDK to delete data. The following examples use the Java SDK.
Delete a single row
-
Delete a row
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));
}
-
Use a condition to delete a row
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
-
Before you delete data, choose a method to query for the primary keys of the rows you want to delete.
-
To delete rows within a specific primary key range, use the GetRange operation to retrieve their primary keys. For more information, see Read data.
-
To delete rows that meet specific criteria, create a search index. Then, use the search index to query for the data and retrieve the primary keys of the rows to be deleted. For more information, see Create a search index and Use an SDK to query data with a search index.
-
Once you have the primary keys, call the BatchWriteRow operation to delete the rows. For more information, see Batch 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 is pk1 and the value of the pk2 primary key column is pk2 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());
}
}
Billing
You are charged for the Capacity Units (CUs) that your operations consume. Depending on the instance type, billing is based on either on-demand or reserved read/write CUs.
The read CUs and write CUs consumed by a DeleteRow operation are calculated as follows:
-
Write CUs consumed: The total size of the primary key columns of the deleted row, divided by 4 KB and rounded up to the nearest integer.
-
Read CUs consumed: If a condition check is specified (not set to IGNORE), the amount is calculated as the total size of the primary key columns, divided by 4 KB and rounded up to the nearest integer.
-
If the operation fails because a row existence condition is not met, it consumes 1 write CU.