Batch delete data

更新时间:
复制 MD 格式

In Tablestore, you can delete data in batches using primary keys. You can also automatically delete expired data by configuring a time to live (TTL).

Important

Deleted data cannot be recovered. Perform this operation with caution.

Directly delete data based on primary keys

Tablestore lets you delete data in batches using primary keys. After you query the primary keys of the data that you want to delete, you can call the BatchWriteRow operation to delete the data. The steps are as follows:

  1. Choose a method to retrieve the primary keys of the data that you want to delete.

    • To delete data within a specific primary key range, call the GetRange operation to query the data and retrieve their primary keys. For more information, see Read data.

    • To delete data that meets specific conditions, you can use a search index if the table has one. Query the data using the search index to retrieve their primary keys. For more information, see Basic queries.

    • To delete all data in a table, you can delete the table and then create a new one with the same configuration.

      Alternatively, you can call the GetRange operation. Set the primary key range from the virtual point INF_MIN (infinity minimum) to INF_MAX (infinity maximum) to scan the entire table and retrieve all primary keys. This operation consumes significant compute resources. Use it with caution.

  2. Call the BatchWriteRow operation to delete the data in batches using the primary keys. For more information, see Write data.

    Note

    The BatchWriteRow operation is limited to 200 rows at a time. However, you can use TableStoreWriter for concurrent write operations. The SDK supports asynchronous deletion. The deletion speed depends on the CPU and network capacity of your application. For more information about concurrent writes, see Use TableStoreWriter for high-concurrency and high-throughput data writes.

Configure TTL to automatically delete expired data

TTL is a table property that defines the data retention period. When data in an attribute column exceeds the configured TTL, the system automatically and asynchronously deletes the expired data from that column. If the data in all attribute columns of a row exceeds the TTL, the system automatically and asynchronously deletes the entire row.

The steps are as follows:

  1. Call the UpdateTable operation to change the table's TTL.

    Important

    If the table has a search index or a secondary index, the TTL value of the index must be less than or equal to the TTL value of the table. When you change the table's TTL, make sure the new TTL value meets this constraint.

  2. Data that exceeds the TTL is considered expired. The system automatically and asynchronously deletes this data.

    Even if the data has not been deleted, it is inaccessible to users.