Created
December 12, 2024 17:57
-
-
Save toabctl/22f10a6c03b1aa7c667cad0067095461 to your computer and use it in GitHub Desktop.
Delete snapshots in region and account
This file contains 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
#!/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