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

/**
 * Delete a row of data. 
 * @api
 * @param [] $request The request parameters. 
 * @return [] The response.  
 * @throws OTSClientException The exception that is returned when a parameter error occurs or the Tablestore server returns a verification error. 
 * @throws OTSServerException The exception that is returned when the Tablestore server returns an error. 
 */
public function deleteRow(array $request);            

Request parameters

ParameterDescription
table_nameThe name of 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.
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.
return_contentThe content that you want to return.

return_type: You can set the value only to ReturnTypeConst::CONST_PK to return the primary key of the row. This parameter is used by the auto-increment primary key column feature.

Request format

$result = $client->deleteRow([
    'table_name' => '<string>', // Specify the name of the data table. 
    'condition' => [
        'row_existence' => <RowExistence>,
        'column_condition' => <ColumnCondition>
    ],
    'primary_key' => [                              // Specify the primary key. 
        ['<string>', <PrimaryKeyValue>], 
        ['<string>', <PrimaryKeyValue>],
        ['<string>', <PrimaryKeyValue>, <PrimaryKeyType>]
    ],
    'return_content' => [
        'return_type' => <ReturnType>
    ]
]);           

Response parameters

ParameterDescription
consumedThe number of CUs that are consumed by the operation.
capacity_unit indicates the number of read and write CUs that are consumed.
  • read: the read throughput
  • write: the write throughput
primary_keyThe value of the primary key, which is consistent with that in the request.
Note If you set return_type to ReturnTypeConst::CONST_PK, the value of the primary key is returned. This parameter is used by the auto-increment primary key column feature.
attribute_columnsThe values of attribute columns, which are consistent with those in the request. At present, the value of this parameter is empty.

Result format

[
    'consumed' => [
        'capacity_unit' => [
            'read' => <integer>,
            'write' => <integer>
        ]
    ],
    'primary_key' => [
        ['<string>', <PrimaryKeyValue>], 
        ['<string>', <PrimaryKeyValue>],
        ['<string>', <PrimaryKeyValue>, <PrimaryKeyType>]
    ],
    'attribute_columns' => []
]            

Examples

  • Example 1

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

    $request = [
        'table_name' => 'MyTable',
        'condition' => RowExistenceExpectationConst::CONST_IGNORE,
        'primary_key' => [ // Specify the primary key. 
            ['PK0', 123],
            ['PK1', 'abc']
        ],
        'return_content' => [
            'return_type' => ReturnTypeConst::CONST_PK     // Specify return_type to return the value of the primary key for auto-increment primary key columns. 
        ]
    ];
    $response = $otsClient->deleteRow($request);            
  • Example 2

    The following code provides an example on how to specify a condition for the DeleteRow operation:

    $request = [
        'table_name' => 'MyTable',
        'condition' => [
            'row_existence' => RowExistenceExpectationConst::CONST_EXPECT_EXIST, // Configure the condition parameter to delete the row when the specified row exists. 
            'column_filter' => [          // Delete the row when the value of the Col0 column is greater than 100. 
                'column_name' => 'Col0',
                'value' => 100,
                'comparator' => ComparatorTypeConst::CONST_GREATER_THAN
            ],
        ],
        'primary_key' => [ // Specify the primary key. 
            ['PK0', 123],
            ['PK1', 'abc']
        ]
    ];
    $response = $otsClient->deleteRow ($request);           

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.