SKM IT World

Just another blog about IT

Groovy Script for Jenkins Script Console to Add or Modify Discard Old Builds Setting


When your Jenkins server runs on for a while, your hard disk usage grows up because the default setting of each job is that every job build and build artifact obtained from this are stored in a build history.  Limiting this build history can save hard disk usage. Each job has a setting option called Discard Old Builds. In this setting you can set how long (in days) old builds keep in the build history (criteria 1) or how many old builds keep at most in the build history (criteria 2).  Jenkins matches first criteria 1 and then criteria 2. Beyond that there exists advanced setting options. This advanced setting options can configured how long and how many builds with build artifacts have to keep in the build history. After that Jenkins deletes the build artifact, but the logs, history, reports, etc for the build will be kept. These will be kept so as long as they match the criteria in the normal setting options.

When you have many jobs, you don’t want to configure every job manually. For that you can modify the Discard Old Builds setting in each job over a Groovy script at one go. Jenkins has a so-called Script Console [1].  In this console you have to put the following Groovy script and every job is modified to discard its old builds.

def daysToKeep = 28
def numToKeep = 10
def artifactDaysToKeep = -1
def artifactNumToKeep = -1

Jenkins.instance.items.each { item ->
  println("JOB: " +
  println("Job type: " + item.getClass())

  if(item.buildDiscarder == null) {
    println("No BuildDiscarder")
    println("Set BuildDiscarder to LogRotator")
  } else {
    println("BuildDiscarder: " + item.buildDiscarder.getClass())
    println("Found setting: " + "days to keep=" + item.buildDiscarder.daysToKeepStr + "; num to keep=" + item.buildDiscarder.numToKeepStr + "; artifact day to keep=" + item.buildDiscarder.artifactDaysToKeepStr + "; artifact num to keep=" + item.buildDiscarder.artifactNumToKeepStr)
    println("Set new setting")

  item.buildDiscarder = new hudson.tasks.LogRotator(daysToKeep,numToKeep, artifactDaysToKeep, artifactNumToKeep)


This script is tested with Jenkins version 1.534 and Jenkins Subversion Plugin version 1.53.

In my last posts ([2], [3]) I showed two other use cases for the Jenkins Script Console. All Groovy scripts can be found in GitHub [4]


[1] Jenkins Script Console
[2] Post about how to rename Subversion host name in every job
[3] Post about how to add or modify Subversion repository browser in every job
[4] GitHub repository with several Groovy scripts for Jenkins script console


15 thoughts on “Groovy Script for Jenkins Script Console to Add or Modify Discard Old Builds Setting

  1. Thanks! This saved plenty of my development time


  2. How to use it in a periodic job?


  3. I did it and are working like a charm.


  4. For some reason I can’t post the modifications to your script, but I adjusted it to take into account folders (free plugin). When processing items in a Jenkins Groovy script one should always check the item type, and if it is a folder, process recursively.


  5. To work with folders (free plugin).

    import hudson.model.*


    def disableChildren(items) {

    def daysToKeep = 90
    def numToKeep = 10
    def artifactDaysToKeep = -1
    def artifactNumToKeep = -1
    for (item in items) {
    if (item.class.canonicalName != ‘com.cloudbees.hudson.plugins.folder.Folder’) {

    println(“JOB: ” +
    println(“Job type: ” + item.getClass())

    if(item.buildDiscarder == null) {
    println(“No BuildDiscarder”)
    println(“Set BuildDiscarder to LogRotator”)
    } else {
    println(“BuildDiscarder: ” + item.buildDiscarder.getClass())
    println(“Found setting: ” + “days to keep=” + item.buildDiscarder.daysToKeepStr + “; num to keep=” + item.buildDiscarder.numToKeepStr + “; artifact day to keep=” + item.buildDiscarder.artifactDaysToKeepStr + “; artifact num to keep=” + item.buildDiscarder.artifactNumToKeepStr)
    println(“Set new setting”)

    item.buildDiscarder = new hudson.tasks.LogRotator(daysToKeep,numToKeep, artifactDaysToKeep, artifactNumToKeep)

    } else {
    disableChildren(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems())


  6. Hi Sandra,

    I am working on migrating and cleaning up of around 350 CI jobs. I am new to groovy scripting. Is there a way I can get a crash course on how to navigate jenkins jobs.

    For ex: After the migration I see that all the jobs have a default JDK in them instead of custom JDK version. I need to script out groovy in such a way that I select my custom JDK from the drop down of JDK list and set that for all the jobs.


  7. May be this would be more accurate question: In the above example which you gave provided (by the way thank u very much I used it and ran it successfully) how did you know item.buildDiscarder is Discard old build check box?

    In eclipse if I do item and hit control space I get the class names. Here how should I find them?


    • Hi Sharat,

      I studied the source code of Jenkins to find out which parameter I have to adjust. I recommend Intellij IDEA Community Edition or Netbeans for Groovy scripting (IMHO they support Groovy better than Eclipse) and set up a Maven project that has Jenkins Core and Jenkins Plugins (that you need) in its dependencies declaration (Here is an POM example).
      I hope that helps.

      Best regards,



  8. Thanks Sandra I will try it out


  9. Fails for me with:

    groovy.lang.MissingPropertyException: No such property: buildDiscarder for class: org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s