Tablestore provides the DeleteRow operation to allow you to delete a single row of data and the BatchWriteRow operation to allow you to delete multiple rows of data in a batch.

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

Prerequisites

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

API operations

"""
Description: This operation deletes a single row of data. 
table_name specifies the name of the table. 
row specifies the row you want to delete, including only the primary key. 
condition is an instance of the tablestore.metadata.Condition class. After a condition is specified, Tablestore checks whether the specified condition is met before Tablestore performs the operation. The operation is performed only when the condition is met. 
You can specify a row existence condition or a condition based on column values. If you specify a row existence condition, you can set condition to IGNORE, EXPECT_EXIST, or EXPECT_NOT_EXIST. 

Response: the number of CUs that are consumed by the operation and the row data that is specified by return_row. 
consumed is an instance of the tablestore.metadata.CapacityUnit class. This parameter indicates the number of CUs that are consumed by the operation. 
return_row indicates the row data that is returned. 
"""
def delete_row(self, table_name, row, condition, return_type = None):                    

Parameters

ParameterDescription
table_nameThe name of the data table.
primary_keyThe primary key of the row.
Note The number and types of the primary key columns that you specify must be the same as the actual number and types of primary key columns in the data table.
conditionThe 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 Configure conditional update.

Examples

The following code provides an example on how to delete a row of data:

primary_key = [('gid',1), ('uid','101')]
row = Row(primary_key)
try:
    consumed, return_row = client.delete_row(table_name, row, None)
# Client exceptions are generally caused by parameter errors or network exceptions. 
except OTSClientError as e:
    print "update row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message())
# Server exceptions are generally caused by parameter or throttling errors. 
except OTSServiceError as e:
    print "update 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())
print ('Delete succeed, consume %s write cu.' % consumed.write)                   

To view the detailed sample code, visit DeleteRow@GitHub.

Delete multiple rows of data in a batch

  1. Select a method to query the primary key information about the rows that you want to delete.
  2. To delete multiple rows of data in a batch based on the primary key information, call the BatchWriteRow operation. For more information, see Write multiple rows of data in a batch.