title: Capistrano recipe to automatically run deploy:cleanup only when needed
body: <p>We do over 20 deployments a day using capistrano (actually <a href="https://github.com/peritor/webistrano/wiki/" rel="nofollow">webistrano</a>) and we have a problem where the disk space on our servers get full of old deployment folders.</p>
  
  <p>Every now and again I run the <code>deploy:cleanup</code> task to clean out all deployments (it keeps the last <code>:keep_releases</code>, currently set to 30). I would like to automate the cleanup. </p>
  
  <p>One solution would be to add the following to the recipe to automatically run the cleanup after every deployment:</p>
  
  <pre><code>after "deploy", "deploy:cleanup"
  </code></pre>
  
  <p><strong>But</strong>, I don't want to do this after <em>every</em> deployment, I'd like to limit it to only when the number of previous deployments gets to a threashold, e.g. 70. Does anyone know how I can do this? </p>
  
  <hr>
  
  <p>Thoughts:</p>
  
  <ul>
  <li>Does Capistrano provide a variable that holds the number of previous deployments?
  <ul>
  <li>If not, does anyone know a way to calculate it. i.e. <code>set :num_releases, &lt;what-can-I-put-here-to-count-previous-deployments&gt;</code></li>
  </ul></li>
  <li>Is there a way to pimp <code>deploy:cleanup</code> so it uses a minimum threshold, i.e. exit if <code>&lt; :max_releases</code> previous deployments (where <code>:max_releases</code> is different from <code>:keep_releases</code>).</li>
  <li>Could the <code>except</code> keyword be used? i.e. something like <code>:except =&gt; { :num_releases &lt; 70}</code>.</li>
  </ul>