Skip to content

Instantly share code, notes, and snippets.

@matiasiglesias
Last active March 12, 2020 13:17
Show Gist options
  • Save matiasiglesias/6adea50b7a93470ef33d6b2c70103d0b to your computer and use it in GitHub Desktop.
Save matiasiglesias/6adea50b7a93470ef33d6b2c70103d0b to your computer and use it in GitHub Desktop.
Script to remove old jobs. Go to Manage Jenkins -> script console and paste this script. Just change MAX_BUILDS for the number of jobs you want to keep
MAX_BUILDS = 10 // max builds to keep
def jobs = Jenkins.instance.items;
def branchesArray = ["master", "develop"] as String[];
def deleteJobs(job) {
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 ""
}
}
}
for (job in jobs) {
println "Job: " + job.name
try {
if (job instanceof jenkins.branch.MultiBranchProject) {
println "Multibranch"
for (branch in branchesArray) {
println "Branch " + branch
job = job.getJob(branch)
deleteJobs(job)
}
} else {
deleteJobs(job)
}
} catch(Exception ex) {
continue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment