Jenkins delete builds older than latest 20 builds for all jobs
Solution 1
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.
Solution 2
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()
}
}
}
Solution 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
Solution 4
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()
}
}
}
}
Solution 5
This can be done in many ways. You can try the following
- 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
Related videos on Youtube
Comments
-
Fadi over 1 year
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.
-
Fadi over 7 yearsI 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.
-
Amit Goldstein over 5 yearsNeed to add import statement on top: import jenkins.model.Jenkins
-
imdibiji over 5 yearsHere are some example scripts from samrocketman: github.com/samrocketman/jenkins-script-console-scripts
-
NickS over 3 yearsI'm wondering how you go around this? java.util.NoSuchElementException at org.jenkinsci.plugins.workflow.cps.persistence.IteratorHack$Itr.next(IteratorHack.java:72)
-
Johnny Doe over 3 years@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() }
-
NickS over 3 yearsThat 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
-
Paul over 2 yearsgetting error "java.lang.NullPointerException: Cannot get property 'builds' on null object " Any help?