AWS
DynamoDB
Database Management
NoSQL
Cloud Storage

DynamoDb Delete all items having same Hash Key

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Amazon DynamoDB is a fully managed NoSQL database service provided by AWS that offers fast and predictable performance with seamless scalability. It is primarily used for applications that require a scalable database solution with low latency and high throughput. DynamoDB organizes data into tables, wherein each table has a primary key consisting of a hash key and an optional range key. One of the common tasks when working with DynamoDB is deleting items that share the same hash key.

Understanding DynamoDB Primary Keys

DynamoDB tables use primary keys to uniquely identify each item in a table. There are two types of primary keys in DynamoDB:

  1. Partition Key (Hash Key): This is a single attribute primary key, which means each item is uniquely identified by a single attribute.
  2. Composite Primary Key: This consists of two attributes - the partition key and the sort key. The partition key determines the partition or the location where the item is stored, and the sort key allows multiple items to have the same partition key while being distinguished by the sort key.

Deleting Items with the Same Hash Key

In scenarios where multiple items have the same hash key (usually those with a composite primary key), you might need to delete all of them. DynamoDB does not provide a built-in batch delete operation for this purpose, so you will need to perform the delete operation programmatically.

Steps for Deleting Items with the Same Hash Key

  1. Query to Retrieve Items: First, retrieve all items with the same hash key. This can be done using the Query operation. Below is an example of how to retrieve items sharing the same hash key:
python
1    import boto3
2
3    dynamodb = boto3.resource('dynamodb')
4    table = dynamodb.Table('YourTableName')
5
6    response = table.query(
7        KeyConditionExpression=Key('hashKeyAttribute').eq('hashKeyValue')
8    )
9
10    items = response['Items']
  1. Batch Delete Operation: DynamoDB does not support a batch delete operation directly through its API. Instead, you can perform a batch write that includes delete operations. The BatchWriteItem API allows up to 25 requests per call. Here's an example in Python:
python
1    with table.batch_writer() as batch:
2        for item in items:
3            batch.delete_item(
4                Key={
5                    'hashKeyAttribute': item['hashKeyAttribute'],
6                    'sortKeyAttribute': item['sortKeyAttribute']
7                }
8            )

Considerations and Caveats

  • Batch Write Limits: The BatchWriteItem API allows for at most 25 items or 16 MB of data per batch. If the data exceeds these limits, multiple batch write operations will be necessary.
  • Provisioned Throughput: Deleting items count against the provisioned write capacity. Ensure your table is configured to handle the delete operations without throttling.
  • Error Handling: Implement error handling to manage any unsuccessful operations, particularly when dealing with throughput exceptions or transient errors.
  • Atomic Operations: Unlike transactions, BatchWriteItem is not atomic. Some deletes could succeed while others fail. Implement checks to handle such scenarios.

Example Table

Below is a summary table of key points covered in this article:

FeatureDescription
Primary KeysPartition key (hash) or composite (hash+sort)
Deletion MethodQuery items and use BatchWriteItem for deletion
Batch LimitsMax of 25 items or 16 MB per BatchWriteItem call Not atomic
ConsiderationsWrite capacity, error handling, atomicity

Conclusion

Deleting items with the same hash key in DynamoDB requires a consistent approach by querying the items first and then utilizing a batch write operation to delete them. While DynamoDB ensures low-latency and high-throughput, carefully structuring your operations with respect to provisioned capacity and considering potential limits will ensure efficiency. By implementing custom logic for error handling and handling partial failures, you can effectively manage the deletion of items and ensure the consistency and reliability of your application.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.