0

Is it possible to delete records from dynamodb based on PrimaryKey and ConditionExpression on SortKey?

Following code sample is throwing an exception for me

 DeleteVideoCall = async function (pk, sk) {
    let params = {
        TableName: this._tableName,
        Key: {
            pk: { S: pk.toString() },
            sk: { S: sk.toString() }
        },
        ConditionExpression: "begins_with(sk,:sk)",
        ExpressionAttributeValues: {
            ":sk" : { S: sk.toString() + "_" }
        }
    };

    return this._ddb
        .deleteItem(params)
        .promise()
        .then((data) => {
            console.log(`Video Call '${pk}/${sk}' deleted`);
            return null;
        })
        .catch((error) => {
            console.error(
                `Error deleting video room '${pk}/${sk}' (${error})`
            );
            throw error;
        });
};

I want to delete all records that begins_with sk and . For example if sk is 560622 then delete all records where sk begins_with 560622

with the code above I get this error:

Error deleting video room '10900/560622' (ConditionalCheckFailedException: The conditional request failed)

1 Answer 1

1

You can't do that. You need whole key to perform a delete. What you can do:

  • query items (limit retrieved properties to your PK and SK)
  • use batch-write to remove multiple items, it also accepts delete requests
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.