How to clean up Amazon EBS Volumes and Snapshots

In this post we we I will show you how to clean up unused Amazon EBS Volumes and Snapshots.

This is worth doing becuase quite a collection can build up over time and Amazon Web Services has limits on the number you can store; these limits can be increased, but you have to make a Request to Increase the Amazon EBS Volume Limit

What are EBS Volumes?

Volumes behave like raw, unformatted block devices, with user supplied device names and a block device interface. You can create a file system on top of Amazon EBS volumes, or use them in any other way you would use a block device (like a hard drive).

What are Snapshots?

Snapshots are a point-in-time of a volume, which are persisted to Amazon S3. These snapshots can be used as the starting point for new Amazon EBS volumes, and protect data for long-term durability. The same snapshot can be used to instantiate as many volumes as you wish. These snapshots can be copied across AWS regions, making it easier to leverage multiple AWS regions for geographical expansion, data center migration and disaster recovery.

Getting started

Create an Amazon Linux instance and upload your EC2 private key and EC2 certificate and then set them

export EC2_PRIVATE_KEY=pk-QWERTYUIOP.pem
export EC2_CERT=cert-QWERTYUIOP.pem

EBS Volumes available to remove

Use this command to list of all the volumes available to remove

ec2-describe-volumes --region eu-west-1 | grep available | awk '{print $2}' | tr '\n' ' '

All Snapshots

Use this command to list of all snapshots

ec2-describe-snapshots --region eu-west-1 | grep SNAPSHOT | awk '{print $2}' | sort | uniq

All Snapshots in use

Use this command to list all snapshots in use

ec2-describe-images --region eu-west-1 | grep BLOCKDEVICEMAPPING | awk '{print $3}' | sort | uniq

All Snapshots not in use by any AMI’s

Use this command to list all snapshots not associated with an AMI

// all snapshots not associated with an AMI
comm -23 <(echo $(ec2-describe-snapshots --region eu-west-1 | grep SNAPSHOT | awk '{print $2}' | sort | uniq) | tr ' ' '\n') <(echo $(ec2-describe-images --region eu-west-1 | grep BLOCKDEVICEMAPPING | awk '{print $3}' | sort | uniq) | tr ' ' '\n') | tr '\n' ' '

Warning

KNOW WHAT YOUR DOING BEFORE RUNNING THE FOLLOWING COMMANDS

How to delete all EBS Volumes which are unattached

Use this command to delete all volumes which are unattached

// delete all volumes which are unattached
ec2-delete-volume --region eu-west-1 $(ec2-describe-volumes --region eu-west-1 | grep available | awk '{print $2}' | tr '\n' ' ')

How to delete all Snapshots which are not in use

Use this command to delete all snapshots not in use

// run as a single command
for s in $(comm -23 <(echo $(ec2-describe-snapshots --region eu-west-1 | grep SNAPSHOT | awk '{print $2}' | sort | uniq) | tr ' ' '\n') <(echo $(ec2-describe-images --region eu-west-1 | grep BLOCKDEVICEMAPPING | awk '{print $3}' | sort | uniq) | tr ' ' '\n') | tr '\n' ' ')
do
	echo Deleting snapshot $s
	ec2-delete-snapshot --region eu-west-1 $s  
done

I take no responsibility nor blame if something goes wrong.

Good Luck



Comments

  1. Wai-Ming Lee May 3rd

    Comment Arrow

    I like script that deletes all un-used snapshots. Very useful and this blog is extremely useful! Thumbs up from me.


  2. Jordan Mance September 23rd

    Comment Arrow

    Thank you for this, this has been extremely helpful and a huge time saver for us.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.