How to write Pipeline to discard old builds?

As for declarative syntax, you can use the options block:

pipeline {
  options {
    buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30'))
  }
  ...
}

Parameters for logRotator (from the source code):

  • daysToKeepStr: history is only kept up to this days.
  • numToKeepStr: only this number of build logs are kept.
  • artifactDaysToKeepStr: artifacts are only kept up to this days.
  • artifactNumToKeepStr: only this number of builds have their artifacts kept.

More information can be found in Cloudbees knowledge base and in the docs for options block.

Leave a Comment