How to delete those old EC2 EBS snapshots

EBS snapshots are a very powerful feature of Amazon EC2. An EBS volume is readily available, elastic block storage device that can be attached, detached and re-attached to any instance in its availability zone. There are numerous advantages to using EBS over the local block storage devices of an instance, and one of the most important of them is the ability to take a snapshot of the data on the volume.

Since snapshots are incremental by nature, after an initial snapshot of a volume, the following snapshots are quick and easy. Moreover, snapshots are always processed by Amazon’s processing power and not by the cpu of your instance, and are stored redundantly on S3. This is why using these snapshots in your backup methodology is a great idea (provided that you freeze/unfreeze your filesystem during the snapshot call, using LVM or XFS for example).

But, and this is a really annoying but – snapshots are “easy come hard to go”. They are so convenient to use and so reliable, that it’s natural to use a cronned script to make a daily, or hell — hourly! — backup of your volume. But then, those snapshots keep piling up, and the only way to delete a snapshot is to call a single API call for a specific snapshot.If you have 5 volumes you back up hourly, you reach the 500 snapshots limit withing 4.5 days. Not very reliable now, huh?

I have been searching for a while for an option to bulk delete snapshots. The EC2 API is missing this feature, and the excellent ElasticFox add-on is not compensating. You just can’t bulk delete snapshots.

That is, until now:). I asked in the AWS Forum if there is anything that can be done about this problem. They replied it’s a good idea, but if I really wanted it to be implemented quickly, I should build my own solution using the API. So I took the offer, and came up with a PHP command line tool that tries to emulate a “ec2-delete-old-snapshots” command, until one is added to the API.

The tool is available on Google Code for checkout. It uses the PHP EC2 library which I bundled in (hope I didn’t break any licensing issue, please alert me if I did).

Usage is easy:

php ec2-delete-old-snapshots.php -v vol-id [-v vol-id ...] -o days

If you wanted to delete ec2 snapshots older than 7 days for 2 volumes you have, you would use:

php ec2-delete-old-snapshots.php -v vol-aabbccdd -v vol-bbccddee -o 7

Hope this helps all you people out there who need such a thing. I will be happy to receive feedback (and bug fixes) if you start using this.

11 thoughts on “How to delete those old EC2 EBS snapshots

  1. It works flawlessly with US snapshots but not with EU snapshots. Any ideas in how to solve this?


  2. Hi Wilson,

    It’s not different by much, they both use the same API call.
    However, this solution let’s you bulk delete by volume ID and limit by date — you can’t do these things with the java ec2-delete-snapshots command line tool.

  3. Trying to execute you script I get the following error:

    php ec2-delete-old-snapshots.php -v vol-3ff83856 -o 0

    Will try to bulk delete for vol-3ff83856 older than 2010/01/30 06:55:35

    Fatal error: Class ‘XsltProcessor’ not found in
    /usr/share/php/Amazon/EC2/Client.php on line 2584

  4. They probably make it easy to create hard to remove, so they stay around, thus filling up the coffers of amazons accounts.

  5. Hello, I am getting this error when running:
    PHP Notice: Undefined variable: go_ahead_volumes in / amazon/ec2-delete-old-snapshots/ec2-delete-old-snapshots.php on line 77
    PHP Warning: in_array () expects parameter 2 to be array, null Given in / amazon/ec2-delete-old-snapshots/ec2-delete-old-snapshots.php on line 77
    You know what can be?

    thank you

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s