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. For more information, see Create a 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
/**
* Delete a single row of data.
* @api
* @param [] $request The request parameters.
* @return [] The response.
* @throws OTSClientException The exception that is thrown when a parameter error occurs or the Tablestore server returns a verification error.
* @throws OTSServerException The exception that is thrown when the Tablestore server returns an error.
*/
public function deleteRow(array $request);
Parameters
Request information
Request parameters
Parameter | Description |
table_name | The name of the data table. |
condition | The condition that must be met to perform the operation. You can specify a row existence condition or a condition based on column values. For more information, see Perform conditional updates. |
primary_key | The primary key of the row. Note 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. |
return_content | The content that you want to return. You can set return_type only to |
Request syntax
$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 information
Response parameters
Parameter | Description |
consumed | The number of capacity units (CUs) that are consumed by the operation. capacity_unit: the number of read/write CUs that are consumed.
|
primary_key | The 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_columns | The values of attribute columns, which are consistent with those in the request. At present, the value of this parameter is empty. |
Response syntax
[
'consumed' => [
'capacity_unit' => [
'read' => <integer>,
'write' => <integer>
]
],
'primary_key' => [
['<string>', <PrimaryKeyValue>],
['<string>', <PrimaryKeyValue>],
['<string>', <PrimaryKeyValue>, <PrimaryKeyType>]
],
'attribute_columns' => []
]
Examples
Delete a row of data
The following sample code provides an example on how to delete a row of data from a table:
$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 when an auto-increment primary key column is specified.
]
];
$response = $otsClient->deleteRow($request);
Specify conditions to delete a row of data
The following sample code provides an example on how to delete a row of data from a table if the row exists and the value of the Col0 column in the row is greater than 100:
$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 at the same time
Select a method to query the primary key information about the data 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 values are within 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 in a batch.
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.