Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from boto3 import resource
- from boto3.dynamodb.conditions import Key
- def delete_all_items(table_name, filter_key=None, filter_value=None):
- client = boto3.client('dynamodb')
- dynamodb = boto3.resource('dynamodb')
- table = dynamodb.Table(table_name)
- response = client.describe_table(TableName=table_name)
- keys = [k['AttributeName'] for k in response['Table']['KeySchema']]
- dynamodb_resource = resource('dynamodb')
- table = dynamodb_resource.Table(table_name)
- if filter_key and filter_value:
- filtering_exp = Key(filter_key).eq(filter_value)
- response = table.scan(FilterExpression=filtering_exp)
- else:
- response = table.scan()
- while True:
- print(len(response['Items']))
- if response.get('LastEvaluatedKey'):
- response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
- with table.batch_writer() as batch:
- for item in response['Items']:
- key_dict = {k: item[k] for k in keys}
- batch.delete_item(Key=key_dict)
- else:
- break
- delete_all_items("MiroData")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement