Skip to content

Instantly share code, notes, and snippets.

@ahelord
Created December 21, 2021 02:47
Show Gist options
  • Save ahelord/286d7136391ad93ae800ae9b16409a9a to your computer and use it in GitHub Desktop.
Save ahelord/286d7136391ad93ae800ae9b16409a9a to your computer and use it in GitHub Desktop.
Delete the versions of the calls except the last 4
from __future__ import absolute_import, print_function, unicode_literals
import boto3
def clean_old_lambda_versions():
client = boto3.client('lambda')
functions = client.list_functions()['Functions']
print("Quantity of functions" + str(len(functions)))
for function in functions:
print(function['Version'])
versions = client.list_versions_by_function(FunctionName=function['FunctionArn'])['Versions']
print("Quantity of versions" + str(len(versions)))
lastFour = versions[-4:]
for version in versions:
if version not in lastFour:
print(version['Version'])
if version['Version'] != function['Version']:
arn = version['FunctionArn']
print('delete_function(FunctionName={})'.format(arn))
client.delete_function(FunctionName=arn) # uncomment me once you've checked
if __name__ == '__main__':
clean_old_lambda_versions()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment