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?


7 Answers 7


Updated answer from the aws docs:

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation bar, verify your region.
  3. In the navigation panel, click AMIs.
  4. Select the AMI, click Actions, and then click Deregister. When prompted for confirmation, click Continue.
  5. In the navigation pane, click Snapshots.
  6. Select the snapshot, click Actions, and then click Delete. When prompted for confirmation, click Yes, Delete.

Hope this help anyone like me! :D

  • 3
    Just to add few cents to Benjamin Smith Max's answer to delete snapshots. You need to first deregister the AMI in the console before the snapshot can be deleted. Jan 19, 2018 at 4:51
  • 3
    If your AMI record still hangs around, just do a reload with bypassing cache: Windows: Ctrl + Shft +R or on macOS: ⌘ + ⌥ + R. May 14, 2018 at 11:45
  • NOTE: It may take a few minutes before the console removes the AMI from the list. Choose Refresh to refresh the status. Aug 22, 2018 at 6:22
  • Very important note as I think. Before deregistering you should save AMI ID somewhere. Otherwise it will be very hard to find snapshot what you need to delete if you have many. Aug 24, 2020 at 12:57
  • I would have done exactly this if it wasn't for Amazon's desire to always give complicated names to things. Why not just name it "Delete" to make it obvious?
    – chx101
    Feb 13 at 12:02

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.

        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))
  • 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. Oct 10, 2012 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, 2012 at 11:23
  • @cyberx86: If a snapshot deletion affected a new volume creation, it would be a bigger problem than just for deleting AMIs. Given how long it takes to populate a volume fully from a snapshot, it should be easy to test with a snapshot full of data. Oct 11, 2012 at 2:12
  • 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, 2012 at 2:24
  • @cyberx86: As I suspected, but nice to know for sure. Thanks for looking it up. Oct 13, 2012 at 18:07

The awscli can also do this.

First get the shapshot id using describe-images:

aws ec2 describe-images --image-ids ami-0123456789

Then deregister the image and delete the snapshot:

aws ec2 deregister-image --image-id ami-0123456789
aws ec2 delete-snapshot --snapshot-id snap-9876543210
  • Remember to describe images and get the snapshot ids of the snapshots. You cannot get it after deregistering the image.
    – look
    Apr 6, 2021 at 22:32

Here's are my 2 cents for the solution, a standalone command to remove a specific ami-id:


# This will delete the given ami-id and it's assosiated snapshots

SNAPSHOTS=$(aws ec2 describe-images --image-ids ${AMI_ID} | jq '.Images[] .BlockDeviceMappings[] .Ebs .SnapshotId')

echo "Deregistering AMI ${AMI_ID}"
aws ec2 deregister-image --image-id ${AMI_ID} >> /dev/null 2>&1

while IFS= read -r line; do
    snapshot_id=$(echo $line | tr -d '"')
    if [[ "${snapshot_id}" != *"snap-"* ]]; then
    echo "Deleting Snapshot ${snapshot_id}"
    aws ec2 delete-snapshot --snapshot-id ${snapshot_id} >> /dev/null 2>&1

done <<< "$SNAPSHOTS"

echo "Done"


$ delete-ami.sh ami-xxxxxx

#put your ami's in ami.txt

for i in `cat ami.txt`;

#put the regions in regions.txt

for r in `cat regions.txt`;do echo $r >>/dev/null;
for e in `cat regions.txt`;do aws ec2 describe-images --image-ids $i --region $r | grep "ImageId\|SnapshotId" | awk -F':' '{print $2}' | tr -d '"',',' | grep "snap" > snapshot.txt;
for s in `cat snapshot.txt`;do aws ec2 deregister-image --image-id $i --region $r;
aws ec2 delete-snapshot --snapshot-id $s --region $r;echo $i $s >> amidel.txt;done;done;

#you can check the ami/snapshots which has been de-registered/deleted

Few days back , I have got same requirment so written small shell script. Note: ami_list.txt file should contain the list of images

while read -r ami_id
    if [[ -n "$ami_id" ]]; then
        echo "describing the image : $ami_id"
        describe_image=$(aws ec2 describe-images --image-ids "$ami_id") 
        response=$(echo "$describe_image"| jq  .Images[].BlockDeviceMappings[].Ebs.SnapshotId | jq . -r)
        aws ec2 deregister-image --image-id "$ami_id"
        for snapshot in $response; do 
            echo "$snapshot"
            aws ec2 delete-snapshot --snapshot-id "$snapshot"
        unset describe_image
done  < ami_list.txt 

sample ami_list.txt file:

$cat ami_list.txt

Use the below script to deregister and delete the ami/snapshots. https://github.com/clone2020/ShellScripts/blob/main/aws_ami_snap_deregister_delete.bash Please go through the README file for using the script. https://github.com/clone2020/ShellScripts/blob/main/README.md

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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