Skip to content

Instantly share code, notes, and snippets.

@pkouman
Created April 27, 2017 20:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pkouman/c987ce8cd622820cce9111ea34662c6b to your computer and use it in GitHub Desktop.
Save pkouman/c987ce8cd622820cce9111ea34662c6b to your computer and use it in GitHub Desktop.
Jenkins - Delete old builds script
MAX_BUILDS = 10 // max builds to keep
def jobs = Jenkins.instance.items;
for (job in jobs) {
println "Job: " + job.name
def recent = job.builds.limit(MAX_BUILDS)
println "Recent Builds: " + recent
println "============================="
for (build in job.builds) {
if (!recent.contains(build) && !build.isBuilding()) {
println "Deleting: " + build
build.delete()
println ""
}
}
}
@genuinefafa
Copy link

just a reminder... access to script console (/script) to run this script...

@kumbasar
Copy link

kumbasar commented Jun 18, 2018

To avoid failures during the removal, I have implemented try/catch block

https://gist.github.com/kumbasar/9f45da525e9cf20ee48e624b2f9533fc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment