SomeGuyOnAComputer
SomeGuyOnAComputer

Reputation: 6208

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

Reference

Upvotes: 4

Views: 37997

Answers (3)

jurasans
jurasans

Reputation: 171

I had a similar issue on windows with powershell. in the end i resorted to this for a string id:

aws dynamodb delete-item --key '{""user_id"":{ ""S"":""myuserid""}}' --table-name users --endpoint-url http://localhost:8000

Upvotes: 2

Frederic Henri
Frederic Henri

Reputation: 53713

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.

Upvotes: 10

Alejandro A.
Alejandro A.

Reputation: 136

I had a similar issue, in my case I found (after a long while) that the key property (and all map properties) should be enclosed in single quotes when input directly in the terminal

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

Upvotes: 1

Related Questions