删除数据

DeleteItem命令用于删除记录,

Deletes in DynamoDB are always singleton operations. There is no single command you can run that would delete all the rows in the table for example.

Remember that item we added to the Reply table in the previous path:

aws dynamodb get-item \
    --table-name Reply \
    --key '{
        "Id" : {"S": "Amazon DynamoDB#DynamoDB Thread 2"},
        "ReplyDateTime" : {"S": "2021-04-27T17:47:30Z"}
    }'

Let’s delete this item. When using the delete-item command we need to reference the full Primary Key just like we do with get-item:

aws dynamodb delete-item \
    --table-name Reply \
    --key '{
        "Id" : {"S": "Amazon DynamoDB#DynamoDB Thread 2"},
        "ReplyDateTime" : {"S": "2021-04-27T17:47:30Z"}
    }'

It’s safe to delete the same item more than once. You can run the same command above as many times as you want and it won’t report an error; if the key doesn’t exist then the DeleteItem API still returns success.

Now that we’ve removed that item from the Reply table we also need to decrement the related Forum Messages count.

aws dynamodb update-item \
    --table-name Forum \
    --key '{
        "Name" : {"S": "Amazon DynamoDB"}
    }' \
    --update-expression "SET Messages = :newMessages" \
    --condition-expression "Messages = :oldMessages" \
    --expression-attribute-values '{
        ":oldMessages" : {"N": "5"},
        ":newMessages" : {"N": "4"}
    }' \
    --return-consumed-capacity TOTAL