title: Ant/Groovy: How to delete files over than given date, but keep a maximum of 3
body: <p>Using code as shown below to age out files over time in a logs directory.  <code>MAX_LOG_FILE_AGE</code> can be calculated or set to any desired value.  This correctly deletes files older than desired.  </p>
  
  <p>The question arises from a change in requirements I received to instead keep only a certain number of the most recent files, in my case three (3).  How might this be achieved?</p>
  
  <pre><code>long deleteDate = System.currentTimeMillis() - MAX_LOG_FILE_AGE
  def ant = new AntBuilder()
  ant.delete() {
  fileset (dir: "${someParentDir}/logs/", includes: '**/*') {
      date(millis: "${deleteDate}", when: 'before')
      }
  }
  </code></pre>