129

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?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Josh
  • 3,601
  • 14
  • 50
  • 71
  • it's ever a good idea to keep some previous releases, then if you last deploy contains a big mistake you can fast rollback with capistrano (cap deploy:rollback) to the previous release until you fix it. – damoiser Dec 05 '13 at 12:00

4 Answers4

155

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

Sharvy Ahmed
  • 7,247
  • 1
  • 33
  • 46
jcrossley3
  • 11,576
  • 4
  • 31
  • 32
  • 2
    May be worth noting that even if the find+rm command fails to remove some files the task seems to silently "succeed" when called via the `after` hook. Calling it directly shows the errors... I realized this when I was starting to run out of disk space. – conny Jul 14 '10 at 10:48
  • 8
    @Josh You accepted this answer, but you should check if you have set the callback for "deploy:cleanup" as I said below, since it don't run by default. – Diego Plentz May 31 '12 at 00:39
134

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.

Diego Plentz
  • 6,760
  • 3
  • 30
  • 31
  • 1
    Thank you, Diego. Your answer works correctly in Capistrano v2.14.1. – scarver2 Jan 30 '13 at 16:28
  • 2
    Looks like [Capistrano 3 calls `deploy:cleanup` as part of the deploy flow](https://github.com/capistrano/capistrano/blob/690afc0c5c3c43d2920b0bfdeb9323c286b12106/lib/capistrano/tasks/deploy.rake#L23). – Dennis Oct 13 '15 at 13:10
42

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

cap deploy:cleanup -s keep_releases=3
jesse cai
  • 583
  • 4
  • 8
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

grappler
  • 527
  • 5
  • 17