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
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created and data is written to the data 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.
API operation
"""
Description: This operation deletes a single row of data.
table_name: the name of the table.
primary_key: the primary key of the row.
condition: the condition that must be met to perform the operation. After you specify a condition, Tablestore checks whether the specified condition is met before Tablestore performs the operation. The operation is performed only if the condition is met. The condition parameter is an instance of the tablestore.metadata.Condition class.
Row existence conditions and conditions based on column values are supported. If you want to specify a row existence condition, you can set the condition parameter to IGNORE, EXPECT_EXIST, or EXPECT_NOT_EXIST based on your business requirements.
Response: the number of capacity units (CUs) consumed by the operation and the row data that is returned, which is indicated by the return_row parameter.
consumed: the number of CUs consumed by the operation. The consumed parameter is an instance of the tablestore.metadata.CapacityUnit class.
return_row: the row data that is returned.
Example:
primary_key = [('gid',1), ('uid',101)]
condition = Condition('IGNORE')
consumed, return_row = client.delete_row('myTable', primary_key, condition)
"""
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. The value of this parameter consists of the name, type, and value of each primary key column. 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 | Yes | The condition that you want to configure to perform the DeleteRow operation. You can configure a row existence condition or a condition based on column values. For more information, see Conditional update. |
return_type | No | The type of the returned data. |
transaction_id | No | The ID of the local transaction. If you want to use the local transaction feature to delete data, you must configure this parameter. |
Examples
The following sample code provides an example on how to delete a single row of data:
# Specify the name of the data table.
table_name = '<TABLE_NAME>'
# Construct the primary key of the row.
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)
# In most cases, client exceptions are caused by parameter errors or network exceptions.
except OTSClientError as e:
print("Delete row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message()))
# In most cases, server exceptions are caused by parameter or throttling errors.
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()))
To view the detailed sample code, visit DeleteRow@GitHub.
Delete multiple rows of data at the same time
Select a method to query the primary key information about the rows that you want to delete.
To delete data whose primary key values are in the specified range, call the GetRange operation to query the data and obtain the primary key information about the data. For more information, see Read data whose primary key value is in a specific range.
To delete data that meets the specified conditions, use search indexes to query the data. Then, obtain the primary key information about the data. For more information, see Create a search index and Use Tablestore SDKs.
Call the BatchWriteRow operation to delete multiple rows of data at the same time based on the primary key information about the rows. For more information, see Write multiple rows of data at the same time.
References
Time to live (TTL) specifies the retention period of data. You can configure TTL for a data table to automatically delete expired data. For more information, see Data versions and TTL.