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.

Note

Rows are basic units of tables. Rows consist of primary keys and attributes. A primary key is required for each row. Rows in a table contain primary key columns of the same names and same data types. Attributes are optional for each row. Rows in a table can also contain different attributes. For more information, see Overview.

Usage notes

  • When you delete data, 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.

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

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.

When you delete a single row of data, you can use the conditional update feature to delete data from the row that meets the specified conditions. For more information, see Conditional update.

Delete multiple rows of data at a time

After you obtain the primary key information of the data that you want to delete, call the BatchWriteRow operation to delete multiple rows of data at a time based on the primary key information.

Note

If you want to delete the data that is written before a specific time, you can use the time to live (TTL) feature when you delete data. For more information, see Data versions and TTL.

You can configure the following settings when you delete multiple rows of data at a time:

  • You can delete data from multiple tables at a time in a request.

  • You can use the conditional update feature to delete data from the row that meets the specified conditions. For more information, see Conditional update.

Usage methods

Use the Tablestore console

You can delete a single row of data or delete multiple rows of data at a time in the Tablestore console.

  1. Log on to the Tablestore console.

  2. On the Overview page, find the instance that you want to manage and click Manage Instance in the Actions column.

  3. On the Tables tab of the Instance Details tab, click the name of the table that you want to manage.

  4. On the Query Data tab of the Manage Table page, select one or more rows that you want to delete and click Delete in the lower part of the page.

  5. In the Delete message, click OK.

Use the Tablestore CLI

You can run the delete command to delete a row of data on the Tablestore CLI.

The following sample code provides an example on how to delete data from the row whose value of the first primary key column is 86 and whose value of the second primary key column is 6771:

delete --pk '["86", 6771]'

Use Tablestore SDKs

You can use Tablestore SDK for Java, Tablestore SDK for Go, Tablestore SDK for Python, Tablestore SDK for Node.js, Tablestore SDK for .NET, or Tablestore SDK for PHP to delete data. In this example, Tablestore SDK for Java is used.

Delete a single row of data

  • 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 when you delete 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 delete the data whose primary key values are in the specified range, call the GetRange operation to query the data and obtain the primary key information of 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. After you obtain the primary key information of the data, call the BatchWriteRow operation to delete multiple rows of data at a time based on the primary key information. For more information, see the Write multiple rows of data at a time 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());
        }
    }

Billing

You are charged based on the number of capacity units (CUs) consumed by an operation. The consumed CUs may include the metered read and write CUs and the reserved read and write CUs based on the instance type.

Note

For more information about instance types and CUs, see Instance and Read and write throughput.

The number of read and write CUs that are consumed by the DeleteRow operation is calculated based on the following rules:

  • The number of consumed write CUs is rounded up from the calculation result of the following formula: Number of consumed write CUs = Size of the data in all primary key columns of the row/4 KB.

  • If the value of the condition parameter is not IGNORE, the number of consumed read CUs is rounded up from the calculation result of the following formula: Number of consumed read CUs = Size of the data in all primary key columns of the row/4 KB.

  • If the specified row existence condition is not met, the operation fails, and one write CU is consumed.