Dynamo Delete Item based on ConditionExpression

0

Hi everyone,

I have been having some issues with deleting an item when pasing ConditionExpressions. This is my params that is passed into the DeleteCommand. I am expecting this to delete the Item below, but everytime, I have been getting ConditionalCheckFailedException: The conditional request failed error.

const input: DeleteCommandInput = { TableName: 'TABLE_NAME', ConditionExpression: 'PK = :v1 and begins_with(SK, :v2)', ExpressionAttributeValues: { ':v1': 'USER#0ee34e40-c739-4bbd-b72b-9361a54db2b5#STUDENT#111', ':v2': 'TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51', }, Key: { PK: 'USER#0ee34e40-c739-4bbd-b72b-9361a54db2b5#STUDENT#111', SK: 'TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51', }, };

This is a sample of the Item that I would like to delete. As you can see, PK = "USER#0ee34e40-c739-4bbd-b72b-9361a54db2b5#STUDENT#111", which is an exact of PK that I am passing in as :v1 and PK above. SK = TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51#DATE#1700671174102", which starts with what I passed in above too.

{ "PK": "USER#0ee34e40-c739-4bbd-b72b-9361a54db2b5#STUDENT#111", "SK": "TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51#DATE#1700671174102", "GSI1PK": "USER#0ee34e40-c739-4bbd-b72b-9361a54db2b5#FORM#E26", "GSI1SK": "TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51#DATE#1700671174102", "type": "ACT", "id": "414fbe9f-1675-47a1-9c3d-f40cb7a25c51", "formId": "E26", "classId": "123", "className": "class1", "studentId": "111", "studentName": "student1", "userId": "0ee34e40-c739-4bbd-b72b-9361a54db2b5", "createdAt": "2023-09-09T23:30:50.565Z" }

Any one have any ideas why this is failing to match? Love to get some suggestions.

Thank you!

DiiiTO
asked 6 months ago360 views
2 Answers
1
Accepted Answer

Not answering the question but why use the PK and SK as the conditions? You're already supplying them as part of the DeleteItem call - you don't need to further specify them as conditions. Conditions are there so that you can specify other attributes in the record.

profile pictureAWS
EXPERT
answered 6 months ago
profile pictureAWS
EXPERT
reviewed 6 months ago
  • Oh because in the table I have SK as TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51#DATE#1700671174102 (composed of testId and Date) and I would like to find SK that begins with TEST#414fbe9f-1675-47a1-9c3d-f40cb7a25c51 (testId only).

    Is this the wrong way about deleting an item? Just want to delete an item that matches PK and SK that starts with something.

  • Just found out that I can't do ConditionExpression of PK and SK. Thank you for the clue

1

While it is possible to use ConditionExpressions with a DeleteItem, I believe your understanding on how it works is wrong. When using a ConditionExpression on a Put/Delete/Update you only evaluate the item which you are operating on, DynamoDB does not look at any other items in the table.

This is the reason why begins_with(SK, :v2) is giving you issues. You already know the sort key begins with that, so there is no need for the condition.

profile pictureAWS
EXPERT
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions