All Products
Search
Document Center

Tablestore:Delete data

Last Updated:Nov 10, 2023

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

The data that you delete cannot be restored. Proceed with caution.

Prerequisites

  • OTSClient is initialized. For more information, see Initialization.
  • A data table is created, and 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.

Parameters

Parameter

Description

tableName

The name of the table.

primaryKey

The primary key information of the row. The value of this parameter contains the name of each primary key column, the type of the primary key column, and the primary key value.

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 conditions 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

Delete a row of data

The following sample code provides an example on how to delete a specific 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 table. 
    RowDeleteChange rowDeleteChange = new RowDeleteChange("<TABLE_NAME>", primaryKey);

    client.deleteRow(new DeleteRowRequest(rowDeleteChange));
}                    

Specify conditions to delete a specific row of data

The following sample code provides an example on how to delete a specific row of data from a table if the specific row exists and the value of the Col0 column in the specific 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 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

  1. Before you delete data, select an appropriate method to query the primary key information of the data that you want to delete based on your business requirements.

    • To query the data whose primary key values are in the specified range and obtain the primary key information of the data, call the GetRange operation. Then, you can delete the data. For more information, see Read data.

    • To delete the data that meets the specified conditions, use search indexes to query the data. Then, obtain the primary key information of the data. For more information, see Create search indexes and Use Tablestore SDKs.

  2. To delete multiple rows of data at a time based on the primary key information, call the BatchWriteRow operation. For more information, see the Write multiple rows of data in a batch section of the "Write data" topic.

    The following sample code provides an example on how to delete a row of data whose value of the pk primary key column is pk in a table and a row of data whose value of the pk1 primary key column is pk1 and whose value of the pk2 primary key column is pk2 in another table at a 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 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 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 batch operation on failed rows. In this example, only the retry request is constructed. 
             * We recommend that you use the custom retry policy in Tablestore SDKs. You can use this policy to retry the batch operation on failed rows. After you configure the retry policy, you do not need to add retry code to call the operation. 
             */
            BatchWriteRowRequest retryRequest = batchWriteRowRequest.createRequestForRetry(response.getFailedRows());
        }
    }