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 from a table. 
// @param DeleteRowRequest           Encapsulate the parameters required to call the DeleteRow operation. 
// @return DeleteRowResponse         The content of the response to the DeleteRow operation. 
DeleteRow(request *DeleteRowRequest) (*DeleteRowResponse, error)                   

Parameters

ParameterDescription
TableNameThe name of the data table.
PrimaryKeyThe 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.
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.

Examples

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

deleteRowReq := new(tablestore.DeleteRowRequest)
deleteRowReq.DeleteRowChange = new(tablestore.DeleteRowChange)
deleteRowReq.DeleteRowChange.TableName = tableName
deletePk := new(tablestore.PrimaryKey)
deletePk.AddPrimaryKeyColumn("pk1", "pk1value1")
deletePk.AddPrimaryKeyColumn("pk2", int64(2))
deletePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
deleteRowReq.DeleteRowChange.PrimaryKey = deletePk
deleteRowReq.DeleteRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
clCondition1 := tablestore.NewSingleColumnCondition("col2", tablestore.CT_EQUAL, int64(3))
deleteRowReq.DeleteRowChange.SetColumnCondition(clCondition1)
_, err := client.DeleteRow(deleteRowReq)
if err != nil {
    fmt.Println("delete failed with error:", err)
} else {
    fmt.Println("delete row finished")
}                    

To view the detailed sample code, visit DeleteRow@GitHub.

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.