DynamoDB
bulk delete
database management
AWS
NoSQL

What is the recommended way to delete a large number of items from DynamoDB?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. When managing large datasets, there may arise a need to delete a significant number of items. The naive approach of iterating through the items and deleting them one by one is inefficient and can run into constraints such as provisioned throughput limits. This article describes the recommended methods for efficiently deleting large numbers of items from DynamoDB.

BatchWriteItem API

DynamoDB provides a BatchWriteItem API that can be used to perform bulk deletes. With this API, you can delete multiple items from one or more tables. The primary advantage is that it reduces the number of round-trips to the service and is more efficient than individual deletions.

How BatchWriteItem Works

  • Batch Limitations: Each batch operation is limited to 25 PutItem or DeleteItem requests.
  • Size Limitations: The total size of all the operations in a single batch must not exceed 16 MB.
  • Unprocessed Items: The API returns any unprocessed items. You will need to handle these by retrying the request.

Example Usage

Here is an example in Python using the AWS SDK (Boto3):

python
1import boto3
2
3# Initialize a session and resource
4dynamodb = boto3.resource('dynamodb')
5table = dynamodb.Table('YourTableName')
6
7# List of items to delete
8keys_to_delete = [{'PrimaryKey': 'Value1'}, {'PrimaryKey': 'Value2'}, ...]
9
10# Partition into batches
11batch_size = 25
12batches = [keys_to_delete[i:i + batch_size] for i in range(0, len(keys_to_delete), batch_size)]
13
14# Perform batch deletions
15for batch in batches:
16    with table.batch_writer() as batch_writer:
17        for key in batch:
18            batch_writer.delete_item(Key=key)

Considerations

  • Error Handling: Always implement a retry mechanism for handling throttled or unprocessed items.
  • Provisioned Throughput: Ensure that you have adequate provisioned throughput, or use DynamoDB's on-demand mode.

Parallel Scan

When you need to delete items based on certain criteria, you may first have to read them using the Scan operation. This can be combined with deletion logic.

Using Parallel Scan

Parallel scans can expedite this process by using multiple threads to scan different segments of a table concurrently.

Example Usage

python
1import boto3
2import threading
3
4def delete_items_from_segment(segment, total_segments):
5    dynamodb = boto3.resource('dynamodb')
6    table = dynamodb.Table('YourTableName')
7    response = table.scan(Segment=segment, TotalSegments=total_segments)
8    
9    while 'Items' in response:
10        for item in response['Items']:
11            # Apply your filter logic to decide if item should be deleted
12            table.delete_item(Key={'PrimaryKey': item['PrimaryKey']})
13
14        # Continuation of the scan
15        if 'LastEvaluatedKey' in response:
16            response = table.scan(Segment=segment, TotalSegments=total_segments, ExclusiveStartKey=response['LastEvaluatedKey'])
17        else:
18            break
19
20# Initiate threads to parallel scan
21total_segments = 4
22threads = []
23for segment in range(total_segments):
24    t = threading.Thread(target=delete_items_from_segment, args=(segment, total_segments))
25    threads.append(t)
26    t.start()
27
28for t in threads:
29    t.join()

Considerations

  • Cost: Scans can be expensive in terms of read capacity units, so it's essential to calculate the cost implications.
  • Throughput: Increasing parallelism will increase throughput, which can lead to throttling if the table limits are not sufficiently high.

Deleting Whole Table

In scenarios where the entire dataset is no longer needed, the most efficient approach is simply to delete the table and recreate it if necessary.

Method

  1. Delete the Table: This operation is immediate and removes all the items in the table.
  2. Recreate the Table: You can recreate it with the desired structure if necessary.

Caution

  • Deleting a table will remove all associated settings, including indexes and any settings for auto-scaling.

Key Points Summary

TopicKey Points
BatchWriteItemBatch up to 25 deletions per operation. Handle unprocessed items. Ensure adequate throughput.
Parallel ScanUse to locate items meeting specific conditions. Can be costly in terms of read capacity. Use threading for parallel operations.
Deleting TableQuickest way to remove all items. Needs restoration of indexes and settings if recreated.

By carefully considering each of these methods, you can ensure efficient management of large deletions within your DynamoDB tables. Each method has its specific use case, and understanding these will help you optimize both performance and cost.


Course illustration
Course illustration

All Rights Reserved.