Skip to content

Instantly share code, notes, and snippets.

@jwilson8767
Created October 26, 2021 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwilson8767/f79d0d8865e5dced6f38dd53b7271da7 to your computer and use it in GitHub Desktop.
Save jwilson8767/f79d0d8865e5dced6f38dd53b7271da7 to your computer and use it in GitHub Desktop.
import boto3
def ddb_delete_all_items(table_id, partition_key, sort_key=None):
"""
Bulk deletion of all items in a DynamoDB Table.
See also:
[AWS REST API doc](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html)
[boto3 doc](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.batch_write_item)
:param table_id: The id of the table.
:param partition_key: The name of the partition key
:param sort_key: The name of the sort key for the table or None.
"""
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_id)
exclusive_start_key = {}
to_delete = []
while True:
response = table.scan(
**exclusive_start_key
)
for item in response['Items']:
delete_key_dict = {
partition_key: item[partition_key]
}
if sort_key is not None:
delete_key_dict[sort_key] = item[sort_key]
to_delete.append({'DeleteRequest': {
'Key': delete_key_dict
}})
exclusive_start_key = {'ExclusiveStartKey': response['LastEvaluatedKey'] if 'LastEvaluatedKey' in response else None}
if exclusive_start_key['ExclusiveStartKey'] is None:
break
failed_to_delete = []
while len(to_delete):
_to_delete = to_delete[:25]
response = dynamodb.batch_write_item(
RequestItems={table_id: _to_delete}
)
if 'UnprocessedItems' in response and table_id in response['UnprocessedItems']:
failed_to_delete += response['UnprocessedItems'][table_id]
to_delete = to_delete[25:]
print(f'The following items could not be deleted from table "{table_id}": {failed_to_delete}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment