Posted - 17/Jan/2024
All the DynamoDB write APIs, including DeleteItem
support criteria-based (conditional) execution. You can use DeleteItem
operation with a condition expression - it must evaluate to true
in order for the operation to succeed.
Here is an example that verifies the value of inactive_days
attribute:
resp, err := client.DeleteItem(context.Background(), &dynamodb.DeleteItemInput{
TableName: aws.String(tableName),
Key: map[string]types.AttributeValue{
"email": &types.AttributeValueMemberS{Value: email},
},
ConditionExpression: aws.String("inactive_days >= :val"),
ExpressionAttributeValues: map[string]types.AttributeValue{
":val": &types.AttributeValueMemberN{Value: "20"},
},
})
if err != nil {
if strings.Contains(err.Error(), "ConditionalCheckFailedException") {
return
} else {
log.Fatal(err)
}
}
Recommended reading - Conditional deletes documentation