Deleting a CloudTrail Trail - 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 a CloudTrail Trail

This example uses the DeleteTrail operation to delete a CloudTrail trail in the us-west-2 region. It requires one input, the name of the trail.

Choose Copy to save the code locally.

Create the file delete_trail.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/cloudtrail" "flag" "fmt" )

Get the name of the trail. If the trail name is missing, display an error message and exit.

trailNamePtr := flag.String("n", "", "The name of the trail to delete") flag.Parse() if *trailNamePtr == "" { fmt.Println("You must supply a trail name") return }

Initialize the session that the SDK uses to load credentials from the shared credentials file .aws/credentials in your home folder and create the client.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }))

Create a CloudTrail client and call DeleteTrail with the trail name. If an error occurs, print the error and exit. If no error occurs, print a success message.

svc := cloudtrail.New(sess) _, err := svc.DeleteTrail(&cloudtrail.DeleteTrailInput{Name: aws.String(*trailNamePtr)}) if err != nil { fmt.Println("Got error calling CreateTrail:") fmt.Println(err.Error()) return } fmt.Println("Successfully deleted trail", *trailNamePtr)

See the complete example on GitHub.