How do I set discard old builds for a Multi Branch Pipeline Job?


  • How do I set discard old builds for a Multi Branch Pipeline Job


  • Pipeline Multibranch Plugin
  • CloudBees Jenkins Enterprise


In order to be able to configure the Discard old builds option within a Multibranch job, there are two main approaches that you might take, depending on whether you are using Scripted Pipeline Syntax or Declarative Pipeline Syntax.

Scripted Pipeline Syntax

For scripted syntax, you can take advantage of the usage of the BuildDiscarderProperty. As you can see in the pipeline syntax reference, by selecting the set job properties step as shown below.

As you can see in the image, when you click on the Generate Groovy button, you get the groovy step that you have to include in your Jenkinsfile.

properties [[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', daysToKeepStr: '10', numToKeepStr: '5']], [$class: 'ScannerJobProperty', doNotScan: false]]

Declarative Pipeline Syntax

In this case, you have available this setting through the options directive, which allow us to set different options such as buildDiscarder, as shown below.

options { buildDiscarder(logRotator(numToKeepStr: '1')) }

You can find additional information on these options in the plugin documentation page

Have more questions? Submit a request


  • 0
    Timothy Reaves

    Using this suggestion leads to invalid code.

  • 1
    David Sanftenberg

    This needs to be configurable at the root level. Adding it into every Jenkinsfile in a Github organization is not feasible when we have hundreds of repos. 

  • 0
    Philip Wofford

    David, love the pic, I've joked about using that image as a "wall paper" of sorts for my entire 'hexicle' (not quite a cube!). I agree, it would make sense for this to be configurable as a default in Jenkins and then allow a Jenkinsfile to override the instance settings. This is a fairly common convention in many places, a hierarchy of command/authority. 

Please sign in to leave a comment.