Skip to content

DynamoDB: delete all items

0

Hello,

Is there a way (direct command/query) to delete all items from a DynamoDB table, from CLI or PartiQL, without using a program ?

Thank you,

asked a year ago742 views

2 Answers
0
Accepted Answer

There is no direct single command to delete all items from a DynamoDB table without using a program. Your options are:

  1. Delete and recreate the table using the AWS CLI:
aws dynamodb delete-table --table-name YourTableName

Then recreate the table with the same schema. This is the most efficient approach if you want to remove all items.

  1. Scan then Delete, this would require you to read all items in the table, and call DeleteItem for each returned result, or with BatchWriteItem in batches of 25.

  2. For batch operations with PartiQL, you would need to specify each item's key:

aws dynamodb batch-execute-statement --statements '[{"Statement": "DELETE FROM \"YourTableName\" WHERE partitionKey = ?", "Parameters": [{"S": "value1"}]}, {"Statement": "DELETE FROM \"YourTableName\" WHERE partitionKey = ?", "Parameters": [{"S": "value2"}]}]'

Note that options 2 and 3 require you to know the keys or attributes of the items you want to delete, which means you would typically need to scan the table first to get this information.

The most straightforward approach is to delete and recreate the table if you want to remove all items.
Sources
Delete DynamoDB data using PartiQL DELETE statements with an AWS SDK - Amazon DynamoDB
Delete DynamoDB data using PartiQL DELETE statements with an AWS SDK - AWS SDK Code Examples
Use DeleteTable with an AWS SDK or CLI - Amazon DynamoDB

answered a year ago

AWS
EXPERT

reviewed a year ago

EXPERT

reviewed a year ago

AWS
EXPERT

revised a year ago

0

If you prefer not to write code or scripts, another option is using a desktop GUI client like DocKit (open source, free) which has a truncate table feature — it handles the scan-then-delete loop for you behind the scenes. Steps:

  1. Connect to your Dynamo
  2. DB table in DocKit.
  3. Open the table in the Manage panel.
  4. Use the truncate option to delete all items

This is essentially the same scan + BatchWriteItem approach from the accepted answer, but without you having to write or run any code. Useful if you need to do this occasionally and don't want to maintain a script.

That said, if you need to do this frequently or at scale (millions of items), the delete and recreate table approach from the accepted answer is still faster — truncation via scan has to paginate through everything, which takes time on large tables.

Download: geekfun.club/products/dockit (https://www.geekfun.club/products/dockit)
Source: github.com/geek-fun/dockit (https://github.com/geek-fun/dockit)

Disclosure: I'm the founder of GEEKFUN, the open-source community behind DocKit.

answered 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.