Skip to content


Repository files navigation

AWSCLEAN - Cleanup unused and old AMIs on AWS

License Apache%202.0 blue link: link: link:

This tool is intended to:

  1. Get all available EC2 instances and get the used AMI

  2. Get all owned AMI’s

  3. Filter out AMI’s which are currently used bei EC2 instances

  4. Delete all AMI’s which are older then the specified duration (default is 7 days)


awsclean [command] [flags]


awsclean uses already provided credentials in ~/.aws/credentials also it uses the central configuration in ~/.aws/config!


awsclean ami scan all AMIs owned by self and delete them if they are unused and older then 7 days.

awsclean ami --account 2451251 scan all AMIs of self and were AWS account 2451251 are owner

awsclean ami --dry-run do not delete anything just show what you would do

awsclean ami --older-then 5w delete all images which are older then 5w and are unused

awsclean ami --ignore ^amia.* --ignore ^amib.* delete all images which name does not start with amia or amib

awsclean ami --launch-templates additionally scan launch templates for used AMIs

awsclean ebs --older-then 5w delete all EBS volumes which are older then 5w and are not bound

awsclean ebs --dry-run do not delete any EBS volume just show what you would do

Filter Logic


all used AMIs are filtered out


all ignore patterns are matched an ignored AMIs are filtered out


the age of the AMI is checked if it’s younger then the given duration the AMI is filtered out


-a, --account string

Set AWS account number to cleanup AMIs. Used to set owner information when selecting AMIs. If not set only 'self' is used.

-d, --dry-run

If set to true nothing will be deleted. And amiclean will just show what it would do!

-o, --older-then string

Set the duration string (e.g 5d, 1w etc.) how old AMIs must be to be deleted. E.g. if set to 7d, AMIs will be delete which are older then 7 days. (default "7d")

-i, --ignore stringArray

Set ignore regex patterns. If a ami name matches the pattern it will be exclueded from cleanup.

-l, --launch-templates

Additionally scan launch templates for used AMIs.

-?, --help

Print usage information

-v, --version

Print version information


Generate mock using mockery

In order to test the ec2client I used mockery to create the mocks:

To generate the mocks. Just run:
go generate ./...

The mockery configuration is done in: