Skip to content

Instantly share code, notes, and snippets.

@serbixote
Last active September 17, 2019 15:23
Show Gist options
  • Save serbixote/4b08ee459adc008d59a7bae7c2f1b82e to your computer and use it in GitHub Desktop.
Save serbixote/4b08ee459adc008d59a7bae7c2f1b82e to your computer and use it in GitHub Desktop.
Delete old builds from all configured Jobs in Jenkins, in a selective way.
import jenkins.model.Jenkins
import hudson.model.Job
int MAX_BUILDS_TO_KEEP = 5
Jenkins.instance.getAllItems(Job.class).each { job ->
job.builds.drop(MAX_BUILDS_TO_KEEP).findAll {
!it.keepLog &&
!it.building &&
it != job.lastStableBuild &&
it != job.lastSuccessfulBuild &&
it != job.lastUnstableBuild &&
it != job.lastUnsuccessfulBuild
}.each { build ->
build.delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment