There are typically 4 steps to what you are looking for:
Terminate instances using the AMI (recommend practise especially for S3 backed AMIs) [Not required before deleting an AMI of any type]
- Deregister AMIs using
- Delete the bundles/snapshots backing the AMI using
ec2-delete-bundle (for S3) or
ec2-delete-snapshot (for EBS).
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:
Unregister an AMI.
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 :
conn = boto.ec2.connect_to_region('your_region', aws_access_key_id='your_key', aws_secret_access_key='your_secret')
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
options, args = parser.parse_args()
sys.stderr.write("Deleting %s and snapshots\n" % str(args))