Tablestore supports two delete operations: DeleteRow to remove a single row, and BatchWriteRow to remove multiple rows at once.
Deleted data cannot be restored.
Prerequisites
An initialized Tablestore client. For details, see Initialize a Tablestore client.
A data table with existing data. For details, see Create a data table and Write data.
Delete a single row
Call the DeleteRow operation to delete one row by its primary key. If the specified row does not exist, the table remains unchanged.
API signature
def delete_row(self, table_name, primary_key, condition, return_type=None, transaction_id=None):Parameters
| Parameter | Required | Description |
|---|---|---|
table_name | Yes | The name of the data table. |
primary_key | Yes | The primary key of the row, specified as a list of (name, value) tuples. The number and types of primary key columns must match the table schema. |
condition | Yes | The condition for the delete operation. Supports row existence conditions (IGNORE, EXPECT_EXIST, EXPECT_NOT_EXIST) and column-value conditions. For details, see Conditional update. |
return_type | No | The type of data to return. |
transaction_id | No | The local transaction ID. Required when using the local transaction feature. |
Response
| Field | Description |
|---|---|
consumed | The capacity units (CUs) consumed by the operation. An instance of tablestore.metadata.CapacityUnit. |
return_row | The returned row data, if requested. |
Example
The following example deletes a single row from a data table:
# Specify the data table name.
table_name = '<TABLE_NAME>'
# Specify the primary key.
primary_key = [('gid', 1), ('uid', '101')]
row = Row(primary_key)
condition = Condition('IGNORE')
try:
consumed, return_row = client.delete_row(table_name, row, condition)
print('Delete succeed, consume %s write cu.' % consumed.write)
except OTSClientError as e:
# Client exceptions are typically caused by invalid parameters or network issues.
print("Delete row failed, http_status:%d, error_message:%s" % (
e.get_http_status(), e.get_error_message()))
except OTSServiceError as e:
# Server exceptions are typically caused by invalid parameters or throttling.
print("Delete row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (
e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))For the complete sample code, see DeleteRow@GitHub.
Delete multiple rows
To delete multiple rows, first retrieve their primary keys, then call BatchWriteRow.
Step 1: Retrieve primary keys
Choose a method based on your use case:
| Use case | Method | Details |
|---|---|---|
| Delete rows within a primary key range | Call GetRange to query and obtain primary keys. | Read data in a range |
| Delete rows matching specific conditions | Use a search index to query matching rows and obtain their primary keys. | Basic query for search index |
| Delete all rows | Delete the table and recreate it with the same configuration. | -- |
To scan all rows, call GetRange with INF_MIN as the start primary key and INF_MAX as the end primary key. This approach consumes significant computing resources. Use it with caution.
Step 2: Delete the rows
Call BatchWriteRow to delete the rows using the primary keys obtained in Step 1. For details, see Write multiple rows.
FAQ
Error when deleting a row with SDK V6.0.0
If an error occurs when you use Tablestore SDK for Python V6.0.0 to delete a single row of data, try one of the following:
Upgrade the SDK to a later version.
Pass
primary_keydirectly without wrapping it in aRowobject:
# Specify the data table name.
table_name = '<TABLE_NAME>'
# Specify the primary key.
primary_key = [('gid', 1), ('uid', '101')]
condition = Condition('IGNORE')
try:
consumed, return_row = client.delete_row(table_name, primary_key, condition)
print('Delete succeed, consume %s write cu.' % consumed.write)
except OTSClientError as e:
print("Delete row failed, http_status:%d, error_message:%s" % (
e.get_http_status(), e.get_error_message()))
except OTSServiceError as e:
print("Delete row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (
e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))Related topics
Data versions and TTL: Configure time to live (TTL) on a data table to automatically delete expired data.