Skip to content

Instantly share code, notes, and snippets.

@toabctl
Created December 12, 2024 17:57
Show Gist options
  • Save toabctl/22f10a6c03b1aa7c667cad0067095461 to your computer and use it in GitHub Desktop.
Save toabctl/22f10a6c03b1aa7c667cad0067095461 to your computer and use it in GitHub Desktop.
Delete snapshots in region and account
#!/usr/bin/python3
"""
Delete all snapshots
!!!!!!!!!!!!!!!! BE CAREFUL !!!!!!!!!!!!!!!!!!!!!!!!!
AWS_DEFAULT_REGION=sa-east-1 AWS_PROFILE=my-profile ./aws-snapshots-delete.py
"""
import concurrent.futures
import boto3
import botocore
ec2res = boto3.resource('ec2')
ec2client = boto3.client('ec2')
region_name = ec2client.meta.region_name
snapshot_list = ec2res.snapshots.filter(OwnerIds=['self'])
snapshot_ids = []
for s in snapshot_list:
snapshot_ids.append(s.id)
print(f'{region_name}: {len(snapshot_ids)} snapshots will be deleted ...')
def _snapshot_delete(snapshot_id):
c = boto3.client('ec2')
try:
#c.delete_snapshot(SnapshotId=snapshot_id)
print(f'{region_name}: {snapshot_id} deleted')
except botocore.exceptions.ClientError as e:
print(e)
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for s_id in snapshot_ids:
futures.append(executor.submit(_snapshot_delete, s_id))
print(f'{region_name}: waiting for {len(futures)} futures to complete ...')
for future in concurrent.futures.as_completed(futures):
res = future.result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment