All Products
Search
Document Center

Tablestore:Delete data

Last Updated:May 31, 2024

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.

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

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 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 Conditional update.

Examples

The following sample code provides an example on how to delete a row from a data table when the row exists and the value of the col2 column is 3:

func DeleteRowWithCondition(client *tablestore.TableStoreClient, tableName string) {
    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
    // If you do not want the system to perform a row existence check, specify RowExistenceExpectation_IGNORE. 
    //deleteRowReq.DeleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    // If the row is expected to exist, specify RowExistenceExpectation_EXPECT_EXIST. 
    deleteRowReq.DeleteRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
    // Specify a condition based on column values. In this example, specify that the condition based on column values is met if the value of the col2 column is 3. 
    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 at the same time

  1. Select a suitable method based on your business requirements to query the primary key information about the data that you want to delete.

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

    The following sample code provides an example on how to delete a row of data whose value of the pk primary key column is pk in a table and a row of data whose value of the pk1 primary key column is pk1 and whose value of the pk2 primary key column is pk2 in another table at a time:

    func BatchWriteRowWithCondition(client *tablestore.TableStoreClient) {
        batchWriteReq := &tablestore.BatchWriteRowRequest{}
    
        deleteRowChange := new(tablestore.DeleteRowChange)
        deleteRowChange.TableName = "<TABLE_NAME1>"
        deletePk := new(tablestore.PrimaryKey)
        deletePk.AddPrimaryKeyColumn("pk", "pk")
        deleteRowChange.PrimaryKey = deletePk
        deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
        batchWriteReq.AddRowChange(deleteRowChange)
    
        deleteRowChange2 := new(tablestore.DeleteRowChange)
        deleteRowChange2.TableName = "<TABLE_NAME2>"
        deletePk2 := new(tablestore.PrimaryKey)
        deletePk2.AddPrimaryKeyColumn("pk1", "pk1")
        deletePk2.AddPrimaryKeyColumn("pk1", "pk2")
        deleteRowChange2.PrimaryKey = deletePk2
        deleteRowChange2.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
        batchWriteReq.AddRowChange(deleteRowChange2)
    
        response, err := client.BatchWriteRow(batchWriteReq)
    	
        if err != nil {
            fmt.Println("batch request failed with:", response)
        } else {
            fmt.Println("batch write row finished")
        }
    }

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.