Search code examples
python-3.xamazon-web-servicesamazon-dynamodbboto3

DynamoDB - boto3 - batch_write_item: The provided key element does not match the schema


This issue has been raised before but so far I couldn't find a solution that worked in boto3. GSI is set on 'solutionId' and partition key being 'emp_id'. Basically, I just want to delete all records in the table without deleting the table itself. What am I missing here?

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.batch_write_item

table_name = "solutions"
dynamodb_client = boto3.client('dynamodb')
dynamodb_resource = boto3.resource('dynamodb')
table = dynamodb_resource.Table(table_name)

data = table.scan()

delete_list = []
for item in data['Items']:
    delete_list.append({
        'DeleteRequest': {
            'Key': {
                'solutionId': {"S": f'{item["solutionId"]}'}
            }
        }
        }
    )


def list_spliter(list, size):
    return (list[pos:pos + size] for pos in range(0, len(list), size))


for batch in list_spliter(delete_list, 25):

    dynamodb_resource.batch_write_item(RequestItems={
        f'{table_name}': batch
    }
    )

Solution

  • I think there are two small issues here:

    1. you're using the high-level service resource interface so you don't need to explicitly tell DynamoDB what the attribute types are. They are inferred through automatic marshaling. So you can simply use "key" : "value" rather than "key": {"S": "value"} for the string keys
    2. when deleting an item you need to provide the full primary key, to include both partition key and sort key

    So, for example, if your partition and sort keys are named pk and sk:

    'DeleteRequest': {
        'Key': {
            'pk': pk,
            'sk': sk
        }
    }