AWS SDK for Go
Developer Guide

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" "os" )

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

var trailName string flag.StringVar(&trailName, "n", "", "The name of the trail to delete") flag.Parse() if trailName == "" { fmt.Println("You must supply a trail name") os.Exit(1) }

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, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create CloudTrail client svc := cloudtrail.New(sess)

Call DeleteTrail with the trail name. If an error occurs, print the error and exit. If no error occurs, print a success message.

_, err = svc.DeleteTrail(&cloudtrail.DeleteTrailInput{Name: trailNamePtr}) if err != nil { fmt.Println("Got error calling CreateTrail:") fmt.Println(err.Error()) os.Exit(1) } fmt.Println("Successfully deleted trail", trailName)

See the complete example on GitHub.