Last active
July 10, 2020 16:43
-
-
Save gmirsky/767cd82d06ceb55c454026b5a262de80 to your computer and use it in GitHub Desktop.
AWS Delete Snapshots without a Volume
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto3 | |
| ec2_resource = boto3.resource('ec2') | |
| # Make a list of existing volumes | |
| all_volumes = ec2_resource.volumes.all() | |
| volumes = [volume.volume_id for volume in all_volumes] | |
| # Find snapshots without an existing volume | |
| snapshots = ec2_resource.snapshots.filter(OwnerIds=['self']) | |
| for snapshot in snapshots: | |
| if snapshot.volume_id not in volumes: | |
| #print(f'Snapshot: {snapshot} volume {snapshot.volume_id} not in volumes') | |
| print(f'aws ec2 delete-snapshot --snapshot-id {snapshot.id}') | |
| #snapshot.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment