AWS DynamoDB with Python (Boto3) — Part 5 — Bulk Insert & Delete with DynamoDB

Vicky AV
2 min readSep 21, 2022
Accessing DynamoDB with Python

If you haven’t read the previous article in this series AWS DynamoDB with Python (Boto3) — Part 4 — Update Attribute & Delete Item from DynamoDB

boto3 client offers support for DynamoDB batch operations via batch_writer() function. Below is the example for bulk insert with DynamoDB

def insert_bulk_student_records(students: list):

dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")

table = dynamodb.Table('Student')

with table.batch_writer() as writer:
for student in students:
writer.put_item(Item={
'dept': student.dept,
'stud_id': student.stud_id,
'stud_name': student.stud_name,
'stud_age': student.stud_age,
'subjects': student.subjects
})

Similarly for bulk delete, we can use the same batch_writer() function

def delete_bulk_student_records(students: list):

dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")

table = dynamodb.Table('Student')

with table.batch_writer() as writer:
for student in students:
writer.delete_item(Key={
'dept': student.dept,
'stud_id': student.stud_id
})

Remember, the constrains we discussed in previous article still applies to batch operations as well

We can mix delete_item() & put_item() in a single batch call

Remember, the batch operation as a whole is not atomic in nature.

Meaning, If we try to insert 10 records & 5 insert calls got successful but the 6th one got failed, doesn’t mean the first 5 inserts will be rolled back

Thats all about Bulk Insert & Delete with DynamoDB !

GitHub Repo URL — https://github.com/iamvickyav/dynamodb-with-python-boto3

--

--