Skip to content

Instantly share code, notes, and snippets.

@filipeandre
Last active February 19, 2025 11:32
Show Gist options
  • Save filipeandre/f02807c4836bc4949a05ad9ae67ebeb0 to your computer and use it in GitHub Desktop.
Save filipeandre/f02807c4836bc4949a05ad9ae67ebeb0 to your computer and use it in GitHub Desktop.
Delete all deprecated lambdas
import boto3
def delete_old_nodejs_lambdas():
client = boto3.client("lambda")
deprecated_runtimes = {"nodejs12.x", "nodejs14.x", "nodejs16.x"}
# Paginate through all Lambda functions
paginator = client.get_paginator("list_functions")
page_iterator = paginator.paginate()
functions_to_delete = []
for page in page_iterator:
for function in page["Functions"]:
function_name = function["FunctionName"]
if "Runtime" in function:
runtime = function["Runtime"]
if runtime in deprecated_runtimes:
functions_to_delete.append(function_name)
print(f"Marked for deletion: {function_name} (Runtime: {runtime})")
# Confirm before deleting
if functions_to_delete:
confirmation = input(f"\nAre you sure you want to delete {len(functions_to_delete)} functions? (yes/no): ").strip().lower()
if confirmation == "yes":
for function_name in functions_to_delete:
delete_lambda(client, function_name)
print("\nDeletion completed.")
else:
print("\nOperation canceled.")
else:
print("No deprecated Node.js functions found.")
def delete_lambda(client, function_name):
"""Deletes a Lambda function, handling both standard and edge functions."""
try:
# List versions to check if it's an edge function with replicas
versions = client.list_versions_by_function(FunctionName=function_name)
for version in versions.get("Versions", []):
version_number = version["Version"]
# Skip $LATEST version
if version_number == "$LATEST":
continue
# Attempt to delete function version
print(f"Deleting version {version_number} of {function_name} ...")
try:
client.delete_function(FunctionName=function_name, Qualifier=version_number)
except client.exceptions.ResourceNotFoundException:
print(f"Version {version_number} of {function_name} already deleted.")
# Delete the primary function
print(f"Deleting function: {function_name} ...")
client.delete_function(FunctionName=function_name)
except Exception as e:
print(f"Error deleting {function_name}: {e}")
if __name__ == "__main__":
delete_old_nodejs_lambdas()
@filipeandre
Copy link
Author

filipeandre commented Feb 17, 2025

curl -s https://gist.githubusercontent.com/filipeandre/f02807c4836bc4949a05ad9ae67ebeb0/raw/delete_deprecated_lambdas.py > delete_deprecated_lambdas.py && python3 ./delete_deprecated_lambdas.py

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