Search code examples
pythonamazon-dynamodbboto3

delete all items DynamoDB using Python


How can I delete all items from DynamoDB using python (boto3)?

I'm trying to do that:

scan = table.scan()
with table.batch_writer() as batch:
  for each in scan['Items']:
    batch.delete_item(Key=each)

But give me this error:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchWriteItem operation: The provided key element does not match the schema

Solution

  • I found a solution! I just mount the key with my table Id and search Id (compId) and It's worked :)

    scan = table.scan()
    with table.batch_writer() as batch:
        for each in scan['Items']:
            batch.delete_item(
                Key={
                    'uId': each['uId'],
                    'compId': each['compId']
                }
            )