DynamoDB CRUD Operations
CRUD stands for Create, Read, Update, and Delete — the four fundamental operations you perform on any database. In DynamoDB, each operation has a dedicated API call with its own rules and behavior. This topic walks through each one with clear examples.
Overview of CRUD in DynamoDB
| Operation | DynamoDB API Call | What It Does |
|---|---|---|
| Create | PutItem | Adds a new item to a table |
| Read | GetItem | Retrieves one item by its primary key |
| Update | UpdateItem | Modifies specific attributes of an existing item |
| Delete | DeleteItem | Removes an item from a table |
DynamoDB also provides BatchWriteItem and BatchGetItem for handling multiple items in one API call.
Create: PutItem
PutItem writes a complete item into the table. If an item with the same primary key already exists, PutItem replaces it entirely.
Example Scenario
A hospital app needs to register a new patient. The table is Patients with PatientID as the partition key.
PutItem Request:
{
"TableName": "Patients",
"Item": {
"PatientID": { "S": "P1001" },
"Name": { "S": "Meera Nair" },
"Age": { "N": "45" },
"BloodGroup": { "S": "O+" },
"WardNumber": { "N": "12" }
}
}
DynamoDB stores this item on the partition that matches the hash of P1001.
Warning: PutItem Overwrites
If you run PutItem again with PatientID: P1001 but different attributes, DynamoDB replaces the entire previous item with the new one. Use UpdateItem instead when you want to change only a few attributes without touching the rest.
Read: GetItem
GetItem retrieves exactly one item using its full primary key. It is the fastest read operation in DynamoDB because it goes directly to the exact partition where the item lives.
Example
GetItem Request:
{
"TableName": "Patients",
"Key": {
"PatientID": { "S": "P1001" }
}
}
Response:
{
"Item": {
"PatientID": { "S": "P1001" },
"Name": { "S": "Meera Nair" },
"Age": { "N": "45" },
"BloodGroup": { "S": "O+" },
"WardNumber": { "N": "12" }
}
}
Projection Expressions
If you only need certain attributes, specify them using a Projection Expression. This reduces the amount of data DynamoDB returns and lowers your read cost.
ProjectionExpression: "Name, BloodGroup"
Response (only requested fields):
{
"Name": { "S": "Meera Nair" },
"BloodGroup": { "S": "O+" }
}
Update: UpdateItem
UpdateItem modifies one or more attributes of an existing item without touching the attributes you do not mention. This makes it safer than PutItem for partial edits.
Update Expressions
DynamoDB uses an Update Expression to describe the change. The most common actions are:
- SET — adds or updates an attribute
- REMOVE — deletes an attribute from the item
- ADD — increments a number or adds to a set
- DELETE — removes elements from a set
Example: Update Ward Number
UpdateItem Request:
{
"TableName": "Patients",
"Key": {
"PatientID": { "S": "P1001" }
},
"UpdateExpression": "SET WardNumber = :newWard",
"ExpressionAttributeValues": {
":newWard": { "N": "7" }
}
}
Only the WardNumber attribute changes. Name, Age, and BloodGroup remain exactly as they were.
Atomic Counter: Increment a Number
UpdateExpression: "ADD ViewCount :inc"
ExpressionAttributeValues: { ":inc": { "N": "1" } }
This safely increments ViewCount by 1 even if multiple users trigger it simultaneously, because DynamoDB handles it atomically.
Delete: DeleteItem
DeleteItem removes a single item from a table using its primary key. If the item does not exist, DynamoDB performs the operation successfully without throwing an error.
Example
DeleteItem Request:
{
"TableName": "Patients",
"Key": {
"PatientID": { "S": "P1001" }
}
}
Patient P1001 is permanently removed from the Patients table.
Batch Operations
BatchGetItem
Retrieves multiple items from one or more tables in a single API call. You can fetch up to 100 items at once. Each item still requires its full primary key.
BatchGetItem fetches P1001, P1002, and P1003 in one round trip instead of three separate GetItem calls.
BatchWriteItem
Puts or deletes up to 25 items in a single call. You cannot update items using BatchWriteItem — use UpdateItem for updates. BatchWriteItem is ideal for loading bulk data.
Diagram: CRUD Flow for a Delivery App
TABLE: Deliveries (PartitionKey: DeliveryID)
Step 1 — PutItem: Add new delivery D500 (Status: "Pending")
↓
Step 2 — GetItem: Driver fetches D500 details
↓
Step 3 — UpdateItem: SET Status = "Out for Delivery"
↓
Step 4 — UpdateItem: SET Status = "Delivered", DeliveredAt = timestamp
↓
Step 5 — DeleteItem: Archive job — remove D500 after 30 days
Return Values
DynamoDB can return the item's data before or after an operation, using the ReturnValues parameter:
- NONE — Return nothing (default)
- ALL_OLD — Return the item as it was before the operation
- ALL_NEW — Return the item as it is after the operation
- UPDATED_OLD — Return only the changed attributes before the update
- UPDATED_NEW — Return only the changed attributes after the update
Summary
| API Call | Use When | Key Behavior |
|---|---|---|
| PutItem | Adding a new item | Replaces entire item if key exists |
| GetItem | Fetching one item | Requires full primary key |
| UpdateItem | Changing specific attributes | Only touches named attributes |
| DeleteItem | Removing an item | No error if item does not exist |
| BatchGetItem | Reading many items at once | Up to 100 items per call |
| BatchWriteItem | Writing or deleting many items | Up to 25 items per call; no updates |
