Delete All Items in DynamoDB Using Java (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
To empty a table while retaining its configuration, scan the complete keys and delete each item. This example is for a controlled maintenance job on a table whose writers have been stopped.
This example uses Java 17+ and the software.amazon.awssdk:dynamodb SDK 2.x dependency. Configure an AWS Region and credentials through your usual AWS profile or runtime role. The Orders table has String keys pk and sk; adapt the table and keys to your schema.
import java.util.*; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.*; public class Example { static AttributeValue s(String value) { return AttributeValue.builder().s(value).build(); } static Map<String, AttributeValue> key(String order) { return Map.of("pk", s("CUSTOMER#123"), "sk", s(order)); } public static void main(String[] args) throws InterruptedException { try (DynamoDbClient ddb = DynamoDbClient.create()) { ScanRequest request = ScanRequest.builder().tableName("Orders") .projectionExpression("#pk, #sk") .expressionAttributeNames(Map.of("#pk", "pk", "#sk", "sk")).build(); for (ScanResponse page : ddb.scanPaginator(request)) { for (Map<String, AttributeValue> itemKey : page.items()) { ddb.deleteItem(DeleteItemRequest.builder() .tableName("Orders").key(itemKey).build()); } } } } }
This is destructive and not atomic. Concurrent writers can add items or change items between the scan and delete. For higher throughput, batch up to 25 delete requests and retry UnprocessedItems.
Reference: AWS Scan API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- Query Local Secondary Index in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Update Item in DynamoDB Using Java
- Delete Expression in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Get All Items from DynamoDB Using Java
- GetItem in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Batch Update Using DynamoDB Mapper
- Delete Multiple Items in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Conditional Update in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Query Global Secondary Index in DynamoDB Using Java
- Remove Item in DynamoDB Using Java
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today