I get the following error when deploying to EB:
ERROR: You cannot have more than 500 Application Versions. Either
remove some Application Versions or request a limit increase.
I went manually and deleted some versions.
I don't want deploys to fail because of this limit.
Is there a way in Elastic Beanstalk to auto-evict unused versions?
A feature was recently added to eb cli (v3.3) to cleanup old versions
https://m.reddit.com/r/aws/comments/340ce0/whats_the_thinking_behind_beanstalks_versioning/
Copying command from reddit link
$ eb labs cleanup-versions --help
usage: eb labs cleanup-versions [options...]
Cleans up old application versions.
optional arguments:
--num-to-leave NUM number of versions to leave DEFAULT=10
--older-than DAYS delete only versions older than x days DEFAULT=60
--force don't prompt for confirmation
You can manage lifecycle policies from the AWS console now.
- Find the Application versions menu in your environment:
- Click on the Settings button on the top right and you'll be able to configure the amount of versions you'd like to keep around:
Note
From the Configuring Application Version Lifecycle Settings documentation:
Elastic Beanstalk applies an application's lifecycle policy each time
you create a new application version, and deletes up to 100 versions
each time the lifecycle policy is applied. Elastic Beanstalk deletes
old versions after creating the new version, and does not count the
new version towards the maximum number of versions defined in the
policy.
Elastic Beanstalk does not delete application versions that are
currently being used by an environment, or application versions
deployed to environments that were terminated less than ten weeks
before the policy was triggered.
The application version limit applies across all applications in a
region. If you have several applications, configure each application
with a lifecycle policy appropriate to avoid reaching the limit.
Elastic Beanstalk only applies the policy if the application version
creation succeeds, so if you have already reached the limit, you must
delete some versions manually prior to creating a new version.
At the time of writing this answer, eb labs cleanup-versions
does not work for me: it returned No application versions to delete
even when I had application versions.
As a workaround, I used this one-liner inspired from this answer (change the region and app name accordingly):
aws elasticbeanstalk describe-application-versions --output text --region=us-west-2 --query 'ApplicationVersions[*].[ApplicationName,VersionLabel,DateCreated]' | grep my-app-name | while read app ver date; do echo "deleting version $app $ver $date" ; aws elasticbeanstalk delete-application-version --region=us-west-2 --application-name $app --version-label $ver --delete-source-bundle; done
There's no built in way to do that, but the following ruby script performs just that. Simply schedule it using cron.
clearnup.rb:
application_name="myApp"
active_versions_shell_output = `aws elasticbeanstalk describe-environments --region=us-east-1 | grep git | awk '{gsub(/.*\:\ \"/,"",$0); print}'`
all_versions_shell_output = `aws elasticbeanstalk describe-applications --region=us-east-1 | grep git | awk '{gsub(/.*\ \"/,"",$0); print}'`
all_versions = all_versions_shell_output.split(/\n/).map{|x| x[0..57]}
active_versions = active_versions_shell_output.split(/\n/).map{|x| x[0..57]}
(all_versions - active_versions).each do |version_to_be_deleted|
puts "deleting #{version_to_be_deleted}"
`aws elasticbeanstalk delete-application-version --delete-source-bundle --application-name #{application_name} --version-label #{version_to_be_deleted}`
end
Now, they have added an admin UI page to delete all application versions:
Managing Application Versions