Skip to content

Instantly share code, notes, and snippets.

@jlcrow
Last active January 18, 2020 15:59
Show Gist options
  • Save jlcrow/22996bfb357c6c57b37cdd908da70da6 to your computer and use it in GitHub Desktop.
Save jlcrow/22996bfb357c6c57b37cdd908da70da6 to your computer and use it in GitHub Desktop.
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
MAX_BUILDS = 20
for (organization in Jenkins.instance.items) {
for (project in organization.items) {
int count = 0
String workspace = project.jobsDir
File folder = new File(workspace)
if(folder!=null && folder.exists()) {
for (folders in folder.listFiles()) {
File builds = new File(folders.path + "/builds")
if (builds!=null && builds.exists()) {
File[] files = builds.listFiles().sort() {
a,b -> b.lastModified().compareTo a.lastModified()
}.each {
if(!it.isFile() && it.name.isNumber()) {
if(count > MAX_BUILDS) {
println it.path + " -- Delete"
it.deleteDir()
}
count++
}
}
}
}
}
}
}
@jlcrow
Copy link
Author

jlcrow commented Jan 8, 2020

Clean out the builds directories to recover space in the Jenkins Home

@jlcrow
Copy link
Author

jlcrow commented Jan 18, 2020

This script is scary btw - ended up with several paused in flight builds losing their jobs folder during an input hold and then failing afterwards

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