Put, get, update and delete items

Amazon DynamoDB · Essentials · 12 min

Single-item operations by key are the bread and butter of every DynamoDB application.

All single-item operations take the full primary key (partition key, plus sort key if the table has one).

  • put_item(Item=...) writes a whole item. If an item with that key exists, it is replaced entirely: attributes you did not include are gone.
  • get_item(Key=...) reads one item. The response has an Item field only if it exists, so use resp.get("Item"). Reads are eventually consistent by default; pass ConsistentRead=True for a strongly consistent read.
  • update_item(Key=..., UpdateExpression=...) changes some attributes in place and creates the item if it does not exist. ReturnValues="ALL_NEW" returns the updated item, saving a separate read.
  • delete_item(Key=...) removes an item; deleting a missing item is not an error.

Each write is atomic for that item, and all four accept a ConditionExpression (covered later) to make the operation happen only if the item is in an expected state.

Remember the asymmetry: put replaces, update merges. Use update_item for counters, status changes and partial edits, and put_item when you own the whole item.

ProjectionExpression on get_item returns only the attributes you list, which saves bandwidth but not read capacity: you pay for the full item size.

Example

import boto3

table = boto3.resource("dynamodb").Table("Orders")
key = {"customer_id": "c1", "order_id": "2025-03-01#A17"}

table.put_item(Item={**key, "total": 35, "status": "PENDING"})

resp = table.update_item(
    Key=key,
    UpdateExpression="SET #s = :s",
    ExpressionAttributeNames={"#s": "status"},      # status is a reserved word
    ExpressionAttributeValues={":s": "SHIPPED"},
    ReturnValues="ALL_NEW",
)
print(resp["Attributes"]["status"])                 # SHIPPED

print(table.get_item(Key=key)["Item"]["total"])     # 35
table.delete_item(Key=key)
print(table.get_item(Key=key).get("Item"))          # None

Remember

  • Every single-item call needs the full primary key
  • put_item replaces the whole item; update_item modifies attributes and upserts
  • get_item returns no Item key when the item does not exist
  • Use ReturnValues to get the new or old item back from a write

Watch out for

  • Using put_item to change one attribute and accidentally wiping all the others.

Check yourself

  1. An item has attributes a, b and c. You call put_item with the same key and only attribute a. What is stored?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html