In my current project, we are heavily using AWS Lambda. We had enabled versioning for the Lambdas. What happened was over the time, the versions got piled up and the maximum storage limit of 75GB was exceeded. This resulted in failed pipelines with codestorageexceeded error.
We had to delete all the old versions quickly. In AWS console, we cannot "Select All" and delete the versions. There was around 105 versions and it will take a lot of time to go to each version and delete it manually.
That is when, I thought about writing a shell script using AWS CLI to get the latest and oldest version number and delete each versions in a loop.
Here is the AWS CLI command to get the latest version:
aws lambda list-versions-by-function --function-name <lambda_function_name_here> --query 'Versions[-1].Version' --output text --region <your region>
If we replace the -1 with 1, we get the oldest version.
aws lambda list-versions-by-function --function-name <lambda_function_name_here> --query 'Versions[1].Version' --output text --region <your region>
And, as an additional bonus, here is the aws command to delete a particular version:
aws lambda delete-function --function-name <aws lambda function name> --region <aws region> --qualifier <version number>