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 and Write data.
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.
*/
deleteRow(params, callback)
Parameters
Parameter | Required | Description |
tableName | Yes | The name of the data table. |
primaryKey | Yes | The primary key information about the row. The primary key information includes the name, type, and value of the primary key column. Important 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. |
condition | Yes | 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 Configure conditional update. |
Examples
The following sample code provides an example on how to delete a row from a data table:
var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');
var params = {
tableName: "sampleTable",
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(8) }, { 'uid': Long.fromNumber(80) }]
};
client.deleteRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
To view the detailed sample code, visit DeleteRow@GitHub.
Delete multiple rows of data at the same time
Select a method to query the primary key information about the rows that 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 at the same time.
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.