Capistrano - clean up old releases

Usually when using capistrano, I will go and manually delete old releases from a deployed application. I understand that you can run cap deploy:cleanup but that still leaves 5 releases. Is this it's intended purpose? Is there another way to cleanup old releases to just 1 previous deploy?


You can use the :keep_releases variable to override the default of 5. Check this out.


You could do this automatically by setting this in your deploy.rb

set :keep_releases, 1
after "deploy:update", "deploy:cleanup" 

In the past(I don't know exactly which version) this callback was the default, but later the developer decided to leave it to the user to decide. In capistrano 3 it was added back to the default deploy flow.


If you want to delete all releases except the last 3 for example you can run:

cap deploy:cleanup -s keep_releases=3

I had a similar problem. I wanted to keep the 5 releases for normal deployments but needed for certain situations to be able to remove all previous releases.

I was able to do this with a custom task. Create a file lib/capistrano/tasks/cleanup.rake and add the following code.

desc "Remove all but the last release"
task :cleanup_all do
    set :keep_releases, 1
    invoke "deploy:cleanup"
end

To run use bundle exec cap staging cleanup_all or cap staging cleanup_all