Delete Queues in AWS SQS using Go

development golang aws sqs

In order to work with AWS SQS using Go, we will use In this gist, we will use the aws-sdk-go-v2 package to delete queues in AWS SQS. The complete source code for the snippet is available here

In order to test the snippet, we will use localstack to run SQS emulator in a docker container on the laptop. The container exposes SQS endpoint on port 4566. The snippet will then delete a queue first-queue in SQS.

In order to start the localstack container, you will need to first install localstack:

$ pip install localstack

Localstack provides a fully functional local AWS cloud stack. It is a cloud service emulator that runs in a single container on a laptop or a development machine.

In order to start the AWS services, run:

$ localstack start -d

You can check the status of the services by running:

$ localstack status services

package main

import (


type SQSQueueAPI interface {
	GetQueueUrl(ctx context.Context,
		params *sqs.GetQueueUrlInput,
		optFns ...func(*sqs.Options)) (*sqs.GetQueueUrlOutput, error)

	DeleteQueue(ctx context.Context,
		params *sqs.DeleteQueueInput,
		optFns ...func(*sqs.Options)) (*sqs.DeleteQueueOutput, error)

func GetQueueURL(c context.Context, api SQSQueueAPI, input *sqs.GetQueueUrlInput) (*sqs.GetQueueUrlOutput, error) {
	return api.GetQueueUrl(c, input)

func DeleteQueue(c context.Context, api SQSQueueAPI, input *sqs.DeleteQueueInput) (*sqs.DeleteQueueOutput, error) {
	return api.DeleteQueue(c, input)

// creates an sqs client.
func client(ctx context.Context, awsURL, region string) *sqs.Client {
	// customResolver is required here since we use localstack and need to point the aws url to localhost.
	customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
		return aws.Endpoint{
			PartitionID:   "aws",
			URL:           awsURL,
			SigningRegion: region,
		}, nil


	// load the default aws config along with custom resolver.
	cfg, err := config.LoadDefaultConfig(ctx, config.WithEndpointResolverWithOptions(customResolver))
	if err != nil {
		log.Fatalf("configuration error: %v", err)

	return sqs.NewFromConfig(cfg)

// delete a queue with the given name.
func deleteQueue(ctx context.Context, c *sqs.Client, queue *string) {
	// Get the URL for the queue
	input := &sqs.GetQueueUrlInput{
		QueueName: queue,
	resultGet, err := GetQueueURL(ctx, c, input)
	if err != nil {
		log.Printf("error getting the queue URL: %v", err)
	queueURL := resultGet.QueueUrl

	// delete the queue using the queue URL
	dqInput := &sqs.DeleteQueueInput{
		QueueUrl: queueURL,
	if _, err := DeleteQueue(ctx, c, dqInput); err != nil {
		log.Printf("error deleting the queue: %v", err)

	log.Printf("deleted queue with URL: %s", *queueURL)

func main() {
	ctx := context.TODO()

	// name of the queue as a command line option.
	queue := flag.String("q", "", "name of the queue")

	// queue cannot be empty string.
	if *queue == "" {
		log.Println("-q argument is required. Specify a name for the queue")

	awsURL := ""
	awsRegion := "us-west-2"

	// create aws client.
	c := client(ctx, awsURL, awsRegion)

	// delete a queue with the given name.
	deleteQueue(ctx, c, queue)


➜  export AWS_ACCESS_KEY_ID=foobar

➜  go build -o sqs .

➜  ./sqs -q third-queue      
2022/09/28 19:37:54 deleted queue with URL:

Get new posts by email


comments powered by Disqus