Miserlou/Zappa

Support for deleting old functions

Alex-Mackie opened this issue · 1 comments

Users are limited to 75GB of Lambda code storage. With multiple daily deployments the 75GB limit can be reached quickly and deleting old deployments via the web console is repetitious.

Would this cleanup belong in Lambda? It could be exposed to users as via a setting like max number of deployed functions to keep.

Here's the script I use (found it somewhere online) for deleting old lambda versions:

import boto3

def clean_old_lambda_versions():
    client = boto3.client('lambda', region_name='<insert_region_name_here>')
    functions = client.list_functions()['Functions']
    for function in functions:
        versions = client.list_versions_by_function(FunctionName=function['FunctionArn'])['Versions']
        for version in versions:
            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()

Before running it, though, please make sure to read through it to not cause any unwanted side effects.
One line is still commented out on purpose.

You could also set it up to run before or after every deployment of your lambda function.

Hope that helps.