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.ReturnValueAllOldthe deleted item comes back inout.Attributes; an empty map means nothing was there. ReturnValuesforDeleteItemsupports onlyNONE(default) andALL_OLD— theALL_NEW/UPDATED_*options belong toUpdateItem.- 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 witherrors.As. DeleteItemremoves one item per call. There is no "delete all" API — to empty a table, iterate keys withScanand 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.
Related examples
- DynamoDB DeleteItem in Java — the same delete with AWS SDK for Java 2.x.
- DynamoDB PutItem in Go — the write side of the same key.
- DynamoDB condition expressions — guard deletes with
attribute_existsand value checks. - DynamoDB ConditionalCheckFailedException — what a failed conditional delete throws, and when it's expected.
References
- DeleteItem — Amazon DynamoDB API Reference
- Use DeleteItem with an AWS SDK or CLI — Amazon DynamoDB Developer Guide
- dynamodb package — AWS SDK for Go v2 (pkg.go.dev)
- dynamodb/types package — AWS SDK for Go v2 (pkg.go.dev)
- Condition expressions — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.