dynobase-icon
Dynobase

Error: dynamodb update fail if not exists

Rafal Wilinski

Answered by Rafal Wilinski

Last updated: October 23rd, 2026

Solution

To make UpdateItem fail when the item does not exist, add attribute_exists to ConditionExpression. UpdateItem otherwise behaves as an upsert. if_not_exists is an UpdateExpression function for default values, not an existence condition.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, UpdateCommand } from "@aws-sdk/lib-dynamodb";
const db = DynamoDBDocumentClient.from(new DynamoDBClient({ region: "us-east-1" }));
await db.send(new UpdateCommand({
  TableName: "Orders",
  Key: { pk: "ORDER#123", sk: "DETAIL" },
  UpdateExpression: "SET #status = :status",
  ConditionExpression: "attribute_exists(#pk)",
  ExpressionAttributeNames: { "#pk": "pk", "#status": "status" },
  ExpressionAttributeValues: { ":status": "SHIPPED" }
}));

The condition is checked atomically against the item identified by the full key. If it is missing, handle ConditionalCheckFailedException as the expected “not found” outcome. A separate GetItem followed by an unconditional update leaves a race where the item can disappear between calls.

See conditional updates and AWS documentation.

Dynobase is a Professional GUI Client for DynamoDB

Start your 7-day free trial today

Product Features

Download
/
Pricing
/
Member Portal
/
Privacy
/
EULA
/
Twitter
© 2026 Dynobase
+
Still using AWS DynamoDB Console?
Try Dynobase to accelerate your DynamoDB workflow. Start your 7-day free trial today.