skip to Main Content

DynamoDB tables can be truncated via AWS console, but I’d like to do this in a script.
All the answers I saw on S.O. regarding this topic involved ‘scans’ and similar stuff that I didn’t completely understand. I’m wondering if there is a simple directive that I can use to accomplish this truncation. Thanks !

3

Answers


  1. Use dynamodbdump

    It has a wipe-data option which is fast and simple and less hassle, you dont have to write any script or any manual effort rather just running this simple command .

    example

    dynamodump wipe-data --throughput 5 --table your-table --region eu-west-1
    

    https://github.com/mifi/dynamodump

    Login or Signup to reply.
  2. You can’t. You need to delete the table. What all the scripts are doing is scanning the entire table and delete each item. Better to delete the table and create it with the same name

    Login or Signup to reply.
  3. While it’s not straight forward, you can combine a couple commands to get you there w/o needing 3rd party tools. What we’re doing here is scanning the table 25 records at a time (the max we can batch execute), using some jmespath shenanigans in the –query to format it into batch execute statements, then passing that to the batch-execute-statement command. Lather, rinse, repeat until the size of bulk.json is 3 bytes or in other words "[]" an empty list.

    Adjust as needed for your table name, sort key, range key (if any), and of course data types (they’re "S" string in this example).

    This works…needs no extra tools…but it’s hardly efficient. If you want to get fancy, recoding it in python using a worker thread pool to pass the batches off to would likely gain it 100x speed.

    > bulk.json # so our first pass doesn't break the while test
    while [ $(du -b bulk.json | cut -f 1) -ne 3 ]; do
        aws dynamodb scan 
            --max-items 25 
            --table     my_table 
            --query "Items[].{"Statement" : join('', ['DELETE FROM "my_table" WHERE "MySortKey" = '',MySortKey.S,'' AND "MyRangeKey" = '', MyRangeKey.S, '''])}" 
            --output json 
            > bulk.json
        aws dynamodb batch-execute-statement 
            --statements file://bulk.json 
            --query "join('', ['Deleted ', to_string(length(Responses)), ' records.'])"; 
            --output text
    done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search