Delete a single row of data from a Tablestore table by using the Python SDK.
Prerequisites
Method
def delete_row(self, table_name, row=None, condition=None, return_type=None, transaction_id=None, **kwargs)
Sample code
The following sample code deletes a row whose primary key value is row1 from the test_table table.
try:
# Construct the primary key.
primary_key = [('id', 'row1')]
# Construct the row to delete.
row = Row(primary_key)
# Call delete_row to delete the row.
consumed, return_row = client.delete_row('test_table', row)
print('Read CU Cost: %s' % consumed.read)
print('Write CU Cost: %s' % consumed.write)
except Exception as e:
print("Delete row failed with error: %s" % e)
FAQ
Error when deleting a single row by using the Tablestore Python SDK 6.0.0
Use one of the following solutions to resolve this issue:
-
Upgrade the Tablestore Python SDK to the latest version.
-
Modify the parameters of the delete method.
# Set the table name. 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('Deletion successful, consumed %s write CU.' % consumed.write) # Handle client exceptions, which are usually parameter errors or network issues. except OTSClientError as e: print("Delete row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message())) # Handle server exceptions, which are usually parameter errors 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()))