Deleting an Amazon DynamoDB Table Item - AWS SDK for Go (version 1)

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Deleting an Amazon DynamoDB Table Item

The following example uses the DynamoDB DeleteItem operation to delete the item with the year 2015 and title The Big New Movie from the Movies table in your default region.

Create the file DynamoDBUpdateItem.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.

import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "fmt" "log" )

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials and region from the shared configuration file ~/.aws/config and create a new DynamoDB service client.

// Initialize a session that the SDK will use to load // credentials from the shared credentials file ~/.aws/credentials // and region from the shared configuration file ~/.aws/config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create DynamoDB client svc := dynamodb.New(sess)

Call DeleteItem to delete the item from the table. If we encounter an error, print the error message. Otherwise, display a message that the item was deleted.

tableName := "Movies" movieName := "The Big New Movie" movieYear := "2015" input := &dynamodb.DeleteItemInput{ Key: map[string]*dynamodb.AttributeValue{ "Year": { N: aws.String(movieYear), }, "Title": { S: aws.String(movieName), }, }, TableName: aws.String(tableName), } _, err := svc.DeleteItem(input) if err != nil { log.Fatalf("Got error calling DeleteItem: %s", err) } fmt.Println("Deleted '" + movieName + "' (" + movieYear + ") from table " + tableName)

See the complete example on GitHub.