Skip to content

Instantly share code, notes, and snippets.

@gmirsky
Last active July 10, 2020 16:43
Show Gist options
  • Select an option

  • Save gmirsky/767cd82d06ceb55c454026b5a262de80 to your computer and use it in GitHub Desktop.

Select an option

Save gmirsky/767cd82d06ceb55c454026b5a262de80 to your computer and use it in GitHub Desktop.
AWS Delete Snapshots without a Volume
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