Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In aws-cli, how to delete items from DynamoDB based on a condition?

I'm using AWS CLI and having issues deleting rows based on a condition

I have a file_name key and I'd like to remove all keys containing "20200322".

Command

aws dynamodb delete-item \
    --table-name "logs" \
    --condition-expression "contains(file_name, :file_name)" \
    --expression-attribute-names file://expression.json \
    --key file://key.json

expression.json - the variables to use in the contains

{
    ":file_name": {
        "S": "20200322"
    }
}

key.json - I don't understand the point of this file

{
    "file_name": {
        "S": "20200322"
    }
}

Error

Parameter validation failed: Invalid type for parameter ExpressionAttributeNames.:file_name, value: OrderedDict([(u'S', u'20200322')]), type: , valid types:

Questions

  • How do I delete a single entry based on a contains condition?
  • Why is the key mandatory if I'm using the --expression-attribute-names switch? What does the key need to be?
  • What is the difference between --expression-attribute-values and --expression-attribute-names

Reference

  • DynamoDB > Condition Expressions
  • DynamoDB > API_DeleteItem_RequestSyntax
like image 527
SomeGuyOnAComputer Avatar asked Sep 01 '25 00:09

SomeGuyOnAComputer


1 Answers

contains function takes 2 parameter: a path and the operand

contains (path, operand)

Here you're missing the operand.

aws dynamodb delete-item \
    --table-name "logs" \
    --key '{"file_name": {"S": "20200322"}}'
    --condition-expression "contains(file_name, :file_name)" \
    --expression-attribute-values file://wvid_logs.json

Note there is double quotes within a pair of single quote.

and in the JSON should be something like

{
    ":file_name": {
        "S": "20200322"
    }
}

The thing is that you want to run a conditional delete, so the key needs to be the key of your item you want to delete, and the expression attribute values will be the condition to check, I am not fully sure you can run a condition on the key itself.

Lets suppose you have

{
    "Id": {
        "N": "12345"
    }
    "file_name": {
        "S": "20200322"
    }
}

running the command

aws dynamodb delete-item \
    --table-name "logs" \
    --key '{"Id": {"N": "12345"}}'
    --condition-expression "contains(file_name, :file_name)" \
    --expression-attribute-values file://wvid_logs.json

The command will delete the item only when the condition from the file matches the item. so if in file you have

{
    ":file_name": {
        "S": "20200322"
    }
}

It will delete the item, any other value in your JSON file will not delete the item.

like image 166
Frederic Henri Avatar answered Sep 03 '25 07:09

Frederic Henri