DynamoDB DeleteItem in Go (AWS SDK v2)

DeleteItem removes a single item by its full primary key. In AWS SDK for Go v2 you call client.DeleteItem with a dynamodb.DeleteItemInput; with types.ReturnValueAllOld the response tells you whether anything was actually deleted.

Code

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

func main() {
	ctx := context.TODO()
	cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-east-1"))
	if err != nil {
		log.Fatalf("load config: %v", err)
	}
	client := dynamodb.NewFromConfig(cfg)

	out, err := client.DeleteItem(ctx, &dynamodb.DeleteItemInput{
		TableName: aws.String("Music"),
		Key: map[string]types.AttributeValue{
			"Artist":    &types.AttributeValueMemberS{Value: "Arturo Sandoval"},
			"SongTitle": &types.AttributeValueMemberS{Value: "Cubano Chant"},
		},
		ReturnValues: types.ReturnValueAllOld,
	})
	if err != nil {
		log.Fatalf("delete item: %v", err)
	}

	if len(out.Attributes) == 0 {
		fmt.Println("No item with that key existed")
	} else {
		fmt.Println("Deleted:", out.Attributes)
	}
}

Explanation

  • Key — must include every primary-key attribute: the partition key alone for a simple key, or partition and sort key for a composite key.
  • Delete is idempotent — deleting a key that doesn't exist is still a success (no error). With ReturnValues: types.ReturnValueAllOld the deleted item comes back in out.Attributes; an empty map means nothing was there.
  • ReturnValues for DeleteItem supports only NONE (default) and ALL_OLD — the ALL_NEW/UPDATED_* options belong to UpdateItem.
  • Conditional delete — add a ConditionExpression (e.g. attribute_exists(Artist) to fail on a missing item, or a value guard); a failed condition returns a *types.ConditionalCheckFailedException, matched with errors.As.
  • DeleteItem removes one item per call. There is no "delete all" API — to empty a table, iterate keys with Scan and batch the deletes, or drop and recreate the table.

Do it visually

Guarding the delete with a condition? The DynamoDB Expression Builder assembles the ConditionExpression and the name/value maps for you.

To delete items from a GUI — select rows, confirm, done, with the generated code one click away — download DynoTable.

References

Last verified 2026-07-13 against the official AWS documentation linked above.

Work with DynamoDB without the Console

DynoTable is a fast desktop client for DynamoDB — browse tables, run SQL-style queries, and edit items locally.