Skip to content

Instantly share code, notes, and snippets.

@nirbhabbarat
Created February 18, 2018 06:48
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save nirbhabbarat/047059f87138e04b2fe83bf678161fff to your computer and use it in GitHub Desktop.
Save nirbhabbarat/047059f87138e04b2fe83bf678161fff to your computer and use it in GitHub Desktop.
Delete AWS volume snapshots older than 30 days via python boto3
#!/usr/bin/env python
##### USE ON YOUR OWN RISK - THIS IS GOING TO delete_snapshot OLDER THAN 30 DAYS
import datetime
import sys
import boto3
age = 30
aws_profile_name = 'prod'
def days_old(date):
date_obj = date.replace(tzinfo=None)
diff = datetime.datetime.now() - date_obj
return diff.days
boto3.setup_default_session(profile_name = aws_profile_name)
ec2 = boto3.client('ec2')
amis = ec2.describe_snapshots(OwnerIds=[
'self'
])
# ], MaxResults=90000)
for ami in amis['Snapshots']:
create_date = ami['StartTime']
snapshot_id = ami['SnapshotId']
day_old = days_old(create_date)
if day_old > age:
try:
print "deleting -> " + snapshot_id + " as image is " + str(day_old) + " old."
# delete the snapshot
ec2.delete_snapshot(SnapshotId=snapshot_id)
except:
print "can't delete " + snapshot_id
@AsTheSeaRises
Copy link

nice - thanks for this.

@chetanr7
Copy link

chetanr7 commented Feb 4, 2019

Can you please suggest that how can we use my aws profile name here.

@gabytal
Copy link

gabytal commented Dec 6, 2021

cool man
works great. thanks!

@tanmay004
Copy link

Hi need help . What is the use of profile name here and how we can use profile name here

@nirbhabbarat
Copy link
Author

@tanmay004 profiles in AWS can be created using aws configure
if you use a default profile, you can pass the default here.

https://gist.github.com/nirbhabbarat/047059f87138e04b2fe83bf678161fff#file-cleanup_aws_volume_snapshot-py-L7

Use case: you might be using multiple accounts, this can help you act on specific account.

@tanmay004
Copy link

#!/usr/bin/env python

USE ON YOUR OWN RISK - THIS IS GOING TO delete_snapshot OLDER THAN 30 DAYS

import datetime
import sys
import boto3
age = 30
aws_profile_name = 'default'
def days_old(date):
date_obj = date.replace(tzinfo=None)
diff = datetime.datetime.now() - date_obj
return diff.days

boto3.setup_default_session(profile_name = aws_profile_name)
ec2 = boto3.client('ec2')
amis = ec2.describe_snapshots(OwnerIds=[
'self'
])
# ], MaxResults=90000)
for ami in amis['Snapshots']:
create_date = ami['StartTime']
snapshot_id = ami['Snaps

@tanmay004 profiles in AWS can be created using aws configure if you use a default profile, you can pass the default here.

https://gist.github.com/nirbhabbarat/047059f87138e04b2fe83bf678161fff#file-cleanup_aws_volume_snapshot-py-L7

Use case: you might be using multiple accounts, this can help you act on specific account.

Hi,

Thank you for your response. I have changed it to default as i am using for one environment but m getting error in line no 25. Could you please help me here i have added ec2 full access in execution role.

#!/usr/bin/env python

USE ON YOUR OWN RISK - THIS IS GOING TO delete_snapshot OLDER THAN 30 DAYS

import datetime
import sys
import boto3
age = 30
aws_profile_name = 'default'
def days_old(date):
date_obj = date.replace(tzinfo=None)
diff = datetime.datetime.now() - date_obj
return diff.days

boto3.setup_default_session(profile_name = aws_profile_name)
ec2 = boto3.client('ec2')
amis = ec2.describe_snapshots(OwnerIds=[
'self'
])
# ], MaxResults=90000)
for ami in amis['Snapshots']:
create_date = ami['StartTime']
snapshot_id = ami['SnapshotId']
day_old = days_old(create_date)
if day_old > age:
try:
print "deleting -> " + snapshot_id + " as image is " + str(day_old) + " old."
# delete the snapshot
ec2.delete_snapshot(SnapshotId=snapshot_id)
except:
print "can't delete " + snapshot_id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment