Our development process makes us deploy very often. As the number of releases grows, junk clogs up the hard drive of our application servers:
- Old release code
- Old
tmp
folders with compiled view templates etc. - Precompiled assets (Javascripts, images...) that no longer exist. When using the asset pipeline, Capistrano will symlink the
public/assets
directory toshared/assets
. This is cool since we can still serve previous assets after a new release, in the window where browser caches might still have references to old assets. But it also means that ourpublic/assets
folder contains every asset we ever compiled.
Eventually your hard drive will run out of disk space because of all that junk. You should frequently remove old releases and assets from your application servers.
As you are lazy (as every developer) you should make Capistrano do this for you.
Capistrano 3
For Capistrano 3 deploy:cleanup
is invoked by default (see
official Documentation
Show archive.org snapshot
). However, it only cleans up releases, not assets.
In order to clean up assets, you can use
capistrano-rails
Show archive.org snapshot
. Just require 'capistrano/rails'
in Capfile and add the following config:
# config/deploy.rb
set :keep_assets, 10
This will keep 10 versions of each asset and remove any older versions.
- If you are using Webpacker, you need to configure capistrano-rails.
- If you are on Rails 3, you cannot use this to clean up assets.
:keep_assets
invokesrake assets:clean[n]
, which in Rails 3 is an alias toassets:clean:all
, which will remove the assets directory altogether Show archive.org snapshot . Either let assets accumulate (which should not be too bad), or implement a custom solution. You may use the original Capistrano 2 implementation of asset expiry Show archive.org snapshot , which was more sophisticated, for inspiration.
Capistrano 2
Open config/deploy.rb
and add this line to the block at the end of the file:
after "deploy:restart", "deploy:cleanup"
Next time you deploy, you will see outlook like this:
** keeping 5 of 27 deployed releases
* executing "rm -rf /path/to/your/releases/directory/..."
** Removing unneeded asset: redmond/images/ui-icons_2e83ff_256x240.png
** Removing unneeded asset: redmond/images/ui-icons_469bdd_256x240-71745de572282e54f00e292afcac7c61.png
** Removing unneeded asset: redmond/images/ui-icons_469bdd_256x240.png
** Removing unneeded asset: redmond/images/ui-icons_6da8d5_256x240-b36f41d94dfba34ce462968f952ec333.png
By default, Capistrano keeps the 5 most recent releases and all assets that have been deployed in the last week.
You can manually trigger the cleanup and also change the default value of 5 releases to keep. Details are described here: Capistrano: Delete old releases