Snapshot Removal: A Guide to Deleting Unused AWS Snapshots

Use this bash script in AWS CloudShell to automatically find and delete EBS snapshots older than a specified date across all AWS regions. This helps reduce storage costs and maintain a clean cloud environment.
Introduction
In the world of cloud computing, the importance of proper housekeeping cannot be overstated. With the dynamic and rapidly evolving digital landscape, it's easy for some things to accumulate and clutter up your resources - like unused AWS snapshots. Left unchecked, these seemingly insignificant elements can add up to significant costs and potentially impact your cloud environment's performance.
Welcome to our step-by-step guide on 'Snapshot Spring Cleaning'. This post will delve into the significance of AWS snapshots, the financial and performance implications of having a slew of unused snapshots, and, most importantly, the process of efficiently clearing them out. Our goal? To ensure your cloud environment is lean, mean, and as efficient as ever.
So grab your digital brooms and dustpans, let's embark on a deep-dive journey into the world of AWS snapshots and declutter your cloud environment together. Because remember - a clean AWS is a happy AWS!
This script permanently deletes snapshots. Always test in a non-production environment first and ensure you have backups of critical data. Deleted snapshots cannot be recovered.
Use This
- AWS CloudShell
- Bash Script
Do This
- Open CloudShell
- Create bash file: touch removeSnapshots.sh
- Create contents of bash file: vi removeSnapshots.sh
- Type in code below
- Save the file
- Change the file to an executable file: chmod +x removeSnapshots.sh
- Run the script: ./removeSnapshots.sh
Before running the delete operation, first run the script with just the describe-snapshots command to preview which snapshots will be affected. This gives you a chance to verify the date filter is working correctly.
Write This
#!/bin/bash
regions=$(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)
for region in $regions
do
echo "Processing region: $region"
snapshotIds=$(aws ec2 describe-snapshots --region $region --owner-ids self --query 'Snapshots[?StartTime<=`2023-01-01`].SnapshotId' --output text)
if [ -n "$snapshotIds" ]; then
echo "Deleting snapshots in region: $region"
for snapshotId in $snapshotIds
do
echo "Deleting snapshot: $snapshotId"
aws ec2 delete-snapshot --region $region --snapshot-id $snapshotId
done
else
echo "No snapshots to delete in region: $region"
fi
done
Modify the date in the query (2023-01-01) to match your retention requirements. The script uses JMESPath query syntax to filter snapshots by their creation date.
What The Heck Does This Code Do
- Gets a list of all AWS regions
- For each of the region found, find snapshots owened by you and snapshots older than a timeframe. In our example, i am looking for all snapshots older than January 1, 2023.
- If the snapshot is found, delete it; otherwise report that there weren't any snapshots and move onto the next region
Troubleshooting
Common Issues and Solutions
Snapshot in Use Error
- Problem: Cannot delete snapshot because it is in use by an AMI
- Solution: First deregister the AMI that uses this snapshot, then retry deletion. Use aws ec2 describe-images to find AMIs using the snapshot.
Permission Denied
- Problem: Access denied when trying to delete snapshots
- Solution: Ensure your IAM user/role has ec2:DeleteSnapshot permission. Check if there are any SCPs or permission boundaries blocking the action.
Script Runs But No Snapshots Deleted
- Problem: Script completes but reports no snapshots found
- Solution: Verify the date filter matches your snapshot dates. Use aws ec2 describe-snapshots without the date filter to see all your snapshots.
CloudShell Session Timeout
- Problem: CloudShell times out before script completes
- Solution: For accounts with many snapshots, run the script for specific regions instead of all regions, or use an EC2 instance with screen/tmux.
Rate Limiting Errors
- Problem: API throttling errors when deleting many snapshots
- Solution: Add a sleep command (sleep 1) between deletions to avoid hitting AWS API rate limits.
Till Next Time
As we conclude this digital decluttering journey, it's vital to remember that effective AWS snapshot management isn't a one-off task but an ongoing process. By regularly identifying and deleting unused AWS snapshots, you not only streamline your cloud environment but also contribute significantly to cost optimization and improved performance.
The steps and strategies outlined in this guide provide an essential foundation to help you maintain a clean, organized, and efficient cloud space. But every cloud environment is unique, so don't hesitate to tailor these approaches to suit your specific needs.
In the dynamic and often complex world of cloud computing, it's the small things that can make a big difference. And remember, consistent cloud cleanliness is next to digital godliness. So, keep your digital broom at the ready, sweep away unnecessary snapshots, and keep your cloud environment running at its peak potential. Until next time, happy 'Snapshot Spring Cleaning'!