17

I am in the process of cleaning up Jenkins (it was setup incorrectly) and I need to delete builds that are older than the latest 20 builds for every job.

Is there any way to automate this using a script or something?

I found many solutions to delete certain builds for specific jobs, but I can't seem to find anything for all jobs at once.

Any help is much appreciated.

6 Answers 6

37

You can use the Jenkins Script Console to iterate through all jobs, get a list of the N most recent and perform some action on the others.

import jenkins.model.Jenkins
import hudson.model.Job

MAX_BUILDS = 20

for (job in Jenkins.instance.items) {
  println job.name

  def recent = job.builds.limit(MAX_BUILDS)

  for (build in job.builds) {
    if (!recent.contains(build)) {
      println "Preparing to delete: " + build
      // build.delete()
    }
  }
}

The Jenkins Script Console is a great tool for administrative maintenance like this and there's often an existing script that does something similar to what you want.

2
14

I got an issue No such property: builds for class: com.cloudbees.hudson.plugins.folder.Folder on Folders Plugin 6.6 while running @Dave Bacher's script

Alter it to use functional api

import jenkins.model.Jenkins
import hudson.model.Job

MAX_BUILDS = 5
Jenkins.instance.getAllItems(Job.class).each { job ->
  println job.name
  def recent = job.builds.limit(MAX_BUILDS)
  for (build in job.builds) {
    if (!recent.contains(build)) {
      println "Preparing to delete: " + build
      build.delete()
    }
  }
}
3
  • 1
    I'm wondering how you go around this? java.util.NoSuchElementException at org.jenkinsci.plugins.workflow.cps.persistence.IteratorHack$Itr.next(IteratorHack.java:72)
    – NickS
    Commented Jul 24, 2020 at 15:12
  • 1
    @NickS Sorry, bro. Haven't haven't seen this issue. According to the code it happens when iterator goes out of bounds github.com/jenkinsci/workflow-cps-plugin/blob/master/src/main/… Probably because of build.delete() triggers modification of origin list backing IteratorHack. Try //before iterating builds: def buildsToDelete = []; ... //in place of deletion: buildsToDelete.add(build) ... // after builds iteration cycle: for (build in buildsToDelete) { build.delete() }
    – Johnny Doe
    Commented Jul 30, 2020 at 13:56
  • That is the solution I ended implementing. Odd how you never got it since it seems like everyone should get it. All good though storing it in a list was good
    – NickS
    Commented Jul 30, 2020 at 15:00
3

There are lots of ways to do this

Personally I would use the 'discard old builds' in the job config

If you have lots of jobs you could use the CLI to step through all the jobs to add it

Alternatively there is the configuration slicing plugin which will also do this for you on a large scale

0
3

For Multibranch Pipelines, I modified the script by Dave Bacher a bit. Use this to delete builds older than the latest 20 build of "master" branches:

MAX_BUILDS = 20

for (job in Jenkins.instance.items) {
  if(job instanceof jenkins.branch.MultiBranchProject) {
    job = job.getJob("master")
    def recent = job.builds.limit(MAX_BUILDS)
    for (build in job.builds) {
      if (!recent.contains(build)) {
        println "Preparing to delete: " + build
        // build.delete()
      }
    }
  }
}
1
  • getting error "java.lang.NullPointerException: Cannot get property 'builds' on null object " Any help?
    – Paul
    Commented Jun 16, 2021 at 17:45
1

This can be done in many ways. You can try the following

  1. get all your job names in a textfile by going to the jobs location in jenkins and run the following

ls >jobs.txt

Now you can write a shell script with a for loop

#!/bin/bash
##read the jobs.txt
for i in 'cat <pathtojobs.txt>'
     do
curl -X POST http://jenkins-host.tld:8080/jenkins/job/$i/[1-9]*/doDeleteAll
     done

the above deletes all the jobs

you can also refer here for more answers

1
  • I like this approach, however my criteria is that I want to make sure that the last 20 builds are available and anything older than this is deleted. Problem with this approach is that it doesn't take into consideration that some jobs have less/more builds than others.
    – Fadi
    Commented Feb 24, 2016 at 21:50
0

I had issues running the suggestions on my Jenkins instance. It could be because it is dockerized. In any case, removing the folder beforehand using the underlying bash interpreter fixes the issue. I also modified the script to keep 180 days of build logs and keep a minimum of 7 build logs:

import jenkins.model.Jenkins
import hudson.model.Job

MIN_BUILD_LOGS = 7

def sixMonthsAgo = new Date() - 180

Jenkins.instance.getAllItems(Job.class).each { job ->
  println job.getFullDisplayName()
  
  def recent = job.builds.limit(MIN_BUILD_LOGS)
  
  def buildsToDelete = job.builds.findAll {
    !recent.contains(it) && ! (it.getTime() > sixMonthsAgo)
  }
  
  if (!buildsToDelete) {
    println "nothing to do"
  }
  for (build in buildsToDelete) {
    println "Preparing to delete: " + build + build.getTime()
    ["bash", "-c", "rm -r " + build.getRootDir()].execute()
    build.delete()
  }
}

"done"

Not the answer you're looking for? Browse other questions tagged or ask your own question.