All Products
Search
Document Center

Tablestore:Delete data

Last Updated:Feb 27, 2026

Tablestore supports two delete operations: DeleteRow to remove a single row, and BatchWriteRow to remove multiple rows at once.

Warning

Deleted data cannot be restored.

Prerequisites

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

ParameterRequiredDescription
table_nameYesThe name of the data table.
primary_keyYesThe 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.
conditionYesThe 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_typeNoThe type of data to return.
transaction_idNoThe local transaction ID. Required when using the local transaction feature.

Response

FieldDescription
consumedThe capacity units (CUs) consumed by the operation. An instance of tablestore.metadata.CapacityUnit.
return_rowThe 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 caseMethodDetails
Delete rows within a primary key rangeCall GetRange to query and obtain primary keys.Read data in a range
Delete rows matching specific conditionsUse a search index to query matching rows and obtain their primary keys.Basic query for search index
Delete all rowsDelete the table and recreate it with the same configuration.--
Note

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_key directly without wrapping it in a Row object:

# 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.