Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know that the command ec2-create-image instance-id will be creating an image of the ec2 instance, creating snapshots file and registering as an AMI. But what is the equivalent command to delete the image which will deleting associated snapshot files and de-registering AMI?

Thanks.

JamesW

share|improve this question

2 Answers 2

There are typically 4 steps to what you are looking for:

  1. Terminate instances using the AMI (recommend practise especially for S3 backed AMIs) [Not required before deleting an AMI of any type]
  2. Deregister AMIs using ec2-deregister
  3. Delete the bundles/snapshots backing the AMI using ec2-delete-bundle (for S3) or ec2-delete-snapshot (for EBS).
  4. Delete EBS volumes (unless they are set to delete on termination, in which case, they would be removed in step #1). This isn't necessary for S3 backed instances. [Again, it is not necessary to terminate instances or delete volumes if you just want to delete an AMI.]

Keep in mind that snapshots and images are independent. You can create an EBS volume from a snapshot and use it as a secondary drive instead of as a boot drive. Furthermore (in the case of Linux instances) it is possible to create a new image from an existing snapshot - which lends reason to the idea that not everyone who wants to delete an image also wants to delete the associated snapshot(s). (Although you can register a snapshot to create a Windows AMI, the AMI isn't launchable.)

It is worth noting that AWS will not let you delete a snapshot associated with an AMI before you deregister the AMI.

Focussing on steps 2 and 3 above, you first need to find the snapshot ID(s) associated with an AMI. This should be listed as part of the block device mappings. Typically, the root EBS volume has the mount point /dev/sda1. You can deregister the AMI from the command line (or use the AWS console) and then delete the snapshot (again, either from the command line or the AWS console).

If you needed to perform this task more often, you would want to script the process. Some libraries such as Python Boto include a function to do exactly this:

deregister_image(image_id, delete_snapshot=False)
    Unregister an AMI.

    Parameters: 
        image_id (string) – the ID of the Image to unregister
        delete_snapshot (bool) – Set to True if we should delete the snapshot associated with an EBS volume mounted at /dev/sda1

For instance a sample script (completely untested, and just cobbled together - use at your own risk!) based on the above might look like :

#!/usr/bin/env python

import os
import sys

def ec2delete(imageid=None):
    conn = boto.ec2.connect_to_region('your_region', aws_access_key_id='your_key', aws_secret_access_key='your_secret') 
    conn.deregister_image(imageid, delete_snapshot=True)

if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()

    options, args = parser.parse_args()
    sys.stderr.write("Deleting %s and snapshots\n" %  str(args))
    ec2delete(args)
share|improve this answer
1  
There is no need to terminate instances of an AMI before deleting the AMI. The AMI is only used to start the instances and they can continue functioning just fine without it. However, if you delete the AMI and the instances fail, then it may be difficult to start replacement instances without the AMI, so be careful. –  Eric Hammond Oct 10 '12 at 8:29
1  
AWS used to recommend terminating instances for a 'complete cleanup', however, in principle it should not be needed for deleting an AMI. I do wonder if it might matter in some edge cases though, since EBS volumes load 'lazily', if a snapshot is deleted shortly after an instance is launched, but before the EBS volume has fully loaded, it could be problematic. –  cyberx86 Oct 10 '12 at 11:23
1  
"Furthermore, it is possible to create a new image from an existing snapshot" -- this only works for Linux AMIs. Windows AMis cannot be created this way. –  Matt Houser Oct 10 '12 at 13:08
    
Absolutely, careless of me to generalize that statement (I am accustomed to working with Linux). I'll make a note of it. –  cyberx86 Oct 10 '12 at 13:21
2  
@EricHammond: a quick search shows that AWS will apparently not delete the snapshot until the data has loaded, but will still let you perform the deletion (presumably simply holding on to the snapshot until it is not needed). –  cyberx86 Oct 11 '12 at 2:24

That would be the not-very-obvious ec2-terminate-instances command. It won't clean up any snapshots, though. For that you'll have to iterate through ec2-describe-snapshots and then use ec2-delete-snapshot on them.

share|improve this answer
    
I believe ec2-terminate-instances command is terminating/deleting the instance from the account. I'm looking for deleting an AMI + the associated snapshot files. –  James W. Oct 10 '12 at 4:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.