Clean up application servers when deploying

Updated . Posted . Visible to the public.

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 to shared/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 our public/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.

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

Thomas Eisenbarth
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Thomas Eisenbarth to makandra dev (2013-05-29 08:23)