Skip to content
Quest 08Intermediate12 min

Release Mew Back to the Wild

It's time to release Mew back into the wild. Deleting data is a serious operation! The DeleteItem operation removes an item from your table. But wait! What if you delete the wrong Pokémon? You should always use a condition to verify what you're deleting.
Start the lesson

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 #name mapped to Name: Only delete if it actually exists.
  • #status = :s (with #status mapped to Status): Only delete if status is "Released".
  • #own = :o (with #own mapped to Owner): Only delete if you are the owner.
Safe Delete ExampleJavaScript
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)"
}));
Your appAWS SDK v3DynamoDBtableP0P1P2P3CONSUMED0

Ready · 5 steps · Orbi

This animation plays when you scroll to it. Press Play to start it now.

All steps as text
  1. Your app sends DeleteItem to the Pokemon table with Name = "Mew". No condition. Mew was traded to Gary a minute ago.
  2. DynamoDB hashes "Mew" to 416a585c, which routes the request to partition P0.
  3. Your app's write to Mew (Owner Gary) lands on P0 and silently replaces the other change.
  4. This call consumed 1 WCU.
  5. 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.
Simulated — 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.