How can I delete multiple Amazon EC2 instance snapshots at the same time?

5 minute read
0

I want to delete a large number of Amazon Elastic Compute Cloud (Amazon EC2) instance snapshots from my AWS account at the same time.

Short description

The Amazon EC2 console allows you to delete up to 50 Amazon Elastic Block Store (Amazon EBS) snapshots at once. To delete more than 50 snapshots, use the AWS Command Line Interface (AWS CLI) or the AWS SDK. For more information on how to delete snapshots, see Delete a snapshot.

If the snapshot is part of an Amazon Machine Image (AMI), then deregister the AMI before you delete the snapshots.

Note: You can't delete public snapshots that other AWS accounts own. You can delete the snapshot only in the account that owns the snapshot.

Resolution

Note: If you receive errors when you run AWS CLI commands, then make sure that you use the most recent version of the AWS CLI.

Use the AWS CLI to delete snapshots from your account

To use the AWS CLI to list, filter, and delete multiple snapshots, complete the following steps:

1.    Run the describe-snapshots command to get a list of your existing snapshots. If you don't include filters on this command, then the list contains all public and private snapshots.

2.    To see snapshots that you own, use the —owner-ids=self filter, or provide the account number of the account that you created snapshots in. In the following example, replace us-west-2 with the AWS Region that you want to check snapshots for:

$ for SnapshotID in $(aws ec2 --region us-west-2 describe-snapshots --owner-ids=1234567891 --query 'Snapshots[*].SnapshotId' --output=text); do  
aws ec2 --region us-west-2 delete-snapshot --snapshot-id ${SnapshotID}  
done

The preceding command lists the snapshots that are in the us-west-2 Region that belong to the specified account. It also lists the snapshots to delete.

3.    To list snapshots that you created from a volume with a size that's greater than 8 GB, run a command similar to the following one:

aws ec2 describe-snapshots --filters Name=volume-id,Values=vol-01234abcdefgh5678 --query 'Snapshots[?(VolumeSize>`8`)].{ID:SnapshotId,ST:StartTime}' --region us-west-2

4.    To get list of snapshots that you created from a volume with a size that's less than 8 GB, use the VolumeSize<`8` parameter.

5.    To get list of snapshots that you created from a volume that's equal to 8 GB, use the VolumeSize==`8` parameter.

6.    To list snapshots with a volume size of 50 GB, make sure that you specify your owner ID. If you don't specify your owner ID with your account number, then public snapshots are also listed. See the following example command:

aws ec2 describe-snapshots --query 'Snapshots[?(VolumeSize==`50`)].{ID:SnapshotId,ST:StartTime}' --region us-west-2 --owner-id 1234567891

7.    To delete snapshots that you created from a volume within a specific time range, use a filter such as StartTime. See the following example command:

aws ec2 describe-snapshots --filters Name=volume-id,Values=vol-01234abcdefgh5678 --query 'Snapshots[?(StartTime >= `2023-01-01`) && (StartTime <= `2050-12-31`)].{ID:SnapshotId,ST:StartTime}' --region us-west-2 | grep snap | awk -F "\"" '{ print $4 }' > snapshot.txt

The preceding command pastes the output snapshot IDs to the snapshot.txt file. Review this file for a list of snapshot IDs, and then delete them.

8.    If the snapshot is part of the AMI, then you must first deregister the AMI. The corresponding snapshot is then deleted. See the following example shell script that takes the data as input, and then generates a list of AMIs and snapshots. It then deregisters the AMIs and snapshots.

Before you run this example script, replace the REGION value with your own Region, and add an input for the deletedata variable. This parameter uses the YYYY-MM-DD format and deregisters all AMI and snapshots that you created before this date. Then, place the bash script in an .sh file, make it executable, and run the script:

#!/bin/bash  
  
REGION=us-east-1  
  
echo "Enter the date in the format YYYY-MM-DD for which you want to delete the AMI and snapshots created prior to this date "  
  
read deletedate  
  
for AMI in $(aws --region $REGION ec2 describe-images --owners 'self' --filters 'Name=name,Values=*' --query 'Images[?CreationDate<`'$deletedate'`] | sort_by(@, &CreationDate)[].ImageId' --output 'text')  
    do  
        echo "$AMI"  
        Snapshots=$(aws ec2 describe-images --image-ids $AMI --region $REGION --query 'Images[].BlockDeviceMappings[*].Ebs.SnapshotId' --output 'text')  
        echo $Snapshots  
        aws ec2 deregister-image --image-id $AMI --region $REGION  
        for SNAPSHOT in $Snapshots  
            do  
                aws --region $REGION ec2 delete-snapshot --snapshot-id $SNAPSHOT  
        done  
done

9.    To describe the list of snapshots that you created in a specific timeframe on a volume, run the following shell script. This script copies the list into a snap-shot-id.txt file, reads the snapshot IDs from the text file, and then deletes them.

Before you run this example script, replace the REGION value with your own Region, and update the start time. Then, place the bash script in an .sh file, make it executable, and run the script:

#!/bin/bash  
  
REGION=us-east-1  
FILE="/tmp/snap-shot-id.txt"  
VOLUMEID="vol-01234abcdefgh5678"  
  
aws ec2 describe-snapshots --filters Name=volume-id,Values=$VOLUMEID --query 'Snapshots[?(StartTime >=`2023-01-01`) && (StartTime <=`2023-08-11`)].{ID:SnapshotId,ST:StartTime}' --region $REGION | grep snap | awk -F "\"" '{ print $4 }' > $FILE  
  
cat $FILE | while read line  
    do  
        echo "Deleting Snapshot: $line"  
        aws ec2 delete-snapshot --region $REGION --snapshot-id $line  
done

Use the AWS SDK to delete snapshots from your account

You can also use AWS SDK to get a list of snapshots, and then delete them. To get a list of your snapshots, use the DescribeSnapshot SDK API call. To delete your snapshots in bulk, use the DeleteSnapshot API call.

AWS OFFICIAL
AWS OFFICIALUpdated 8 months ago