Delete Files From AWS S3 using Go

development golang aws

In order to work with AWS S3 using Go, we will use In this gist, we will use the aws-sdk-go package to delete all files under a given directory in a given bucket. The complete source code for the snippet is available here

In order to test the snippet, we will use localstack to run s3 emulator in a docker container on the laptop. The container exposes s3 endpoint on port 4566. The snippet will then create a new bucket work-with-s3 in s3. The snippet will then delete all files from blog directory under work-with-s3 bucket.

In order to start the localstack container, run:

$ docker-compose up --build --remove-orphans -d

In order to stop the container, run:

$ docker-compose down --remove-orphans

package main

import (


func main() {
	ctx := context.Background()
	bucket := "work-with-s3"

	conf := aws.NewConfig().

	sess, err := session.NewSession(conf)
	if err != nil {
		log.Fatalf("failed to create a new aws session: %v", sess)

	s3client := s3.New(sess)

	// create a new s3 bucket.
	if _, err := s3client.CreateBucket(&s3.CreateBucketInput{Bucket: aws.String(bucket)}); err != nil {
		if aerr, ok := err.(awserr.Error); ok && aerr.Code() != "Conflict" && aerr.Code() != "BucketAlreadyOwnedByYou" {
			log.Fatalf("failed to create a new s3 bucket: %v", err)

	// iterator to delete all files under `blog` directory.
	iter := s3manager.NewDeleteListIterator(s3client, &s3.ListObjectsInput{
		Bucket: aws.String(bucket),
		Prefix: aws.String("blog/"),

	// use the iterator to delete the files.
	if err := s3manager.NewBatchDeleteWithClient(s3client).Delete(ctx, iter); err != nil {
		log.Fatalf("failed to delete files under given directory: %v", err)

Get new posts by email


comments powered by Disqus