Cleanup S3 buckets with Go & Cobra CLI

Andrew Larsen
9 min readDec 19, 2023

As part of our development of Compoze.io we spin up and tear down AWS infrastructure frequently. While Compoze itself will cleanup many resources for you, there are some long term storage services that we don’t want to assume users will want deleted. This is particularly important for high compliance environments that may need to keep data around for years at a time.

One service that we seem to frequently need to cleanup manually are S3 buckets. After a while, we got pretty sick of going to the S3 console to empty and delete buckets after we were done. In an effort to keep my Go skills sharp, I decided to build a quick command line utility to make this cleanup processes easier. Below I’ll walk through how you can build something like this yourself!

Project Setup

Let’s walkthrough the following steps:

  1. Initialize Go project
  2. Install Go Dependencies
  3. Write our application logic

Initialize Go project

First, let’s create an empty directory and setup out project:

mkdir s3-cleanup-command
cd s3-cleanup-command
go mod init s3-cleanup-command

We should now have a s3-cleanup-command directory with a fresh go.mod file

Install Go Dependencies

Now let’s install the required dependencies. Run the following commands:

--

--