What you'll learn
- DeleteItem permanently removes an item from the table.
- Use ConditionExpression to ensure you delete the correct item.
- A common pattern is checking attribute_exists() or matching a version number.
- Need an undo button? A soft delete just sets a flag like
#status = "Released"(or a TTL timestamp) instead of removing the item.
Section 01
The Delete Operation
DeleteItem is straightforward: give it a key, and the item is gone. However, it is idempotent: if you delete an item that doesn't exist, DynamoDB still returns success!
Section 02
Safe Deletes
To prevent accidents (like deleting a Pokémon that isn't yours, or one that was already transferred), use a ConditionExpression.
Common checks include:
attribute_exists(#name), with#namemapped toName: Only delete if it actually exists.#status = :s(with#statusmapped toStatus): Only delete if status is "Released".#own = :o(with#ownmapped toOwner): Only delete if you are the owner.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, DeleteCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({ region: "ap-south-1" });
const docClient = DynamoDBDocumentClient.from(client);
await docClient.send(new DeleteCommand({
TableName: "Pokemon",
Key: { Name: "Mew" },
ExpressionAttributeNames: { "#name": "Name" },
ConditionExpression: "attribute_exists(#name)"
}));Ready · 5 steps · Orbi
This animation plays when you scroll to it. Press Play to start it now.
All steps as text
- Your app sends DeleteItem to the Pokemon table with Name = "Mew". No condition. Mew was traded to Gary a minute ago.
- DynamoDB hashes "Mew" to 416a585c, which routes the request to partition P0.
- Your app's write to Mew (Owner Gary) lands on P0 and silently replaces the other change.
- This call consumed 1 WCU.
- DynamoDB responds with success: Gary's Mew is gone and nobody was warned.
Practice
Your turn
Time to say goodbye to Mew. Use
DeleteCommand with a ConditionExpression that only deletes Mew if it exists and still belongs to Ash: attribute_exists(#name) AND #own = :trainer, with #name → Name, #own → Owner (also reserved) and :trainer bound to "Ash".Use
DeleteCommand with a ConditionExpression to remove Mew safely.Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the code.
Ctrl/⌘ Enter to run · simulated, no AWS callsSimulated — no AWS calls
Check
Check your understanding
Quick Quiz · one question, as many tries as you need.
What happens if you run DeleteItem on a key that doesn't exist (without a condition)?
Choose one answer.