Skip to content

Instantly share code, notes, and snippets.

@bsnux
Created March 9, 2022 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsnux/403626a87d589ceb565338ca4003a0da to your computer and use it in GitHub Desktop.
Save bsnux/403626a87d589ceb565338ca4003a0da to your computer and use it in GitHub Desktop.
Deletes old builds leaving only the ones given by MAX_TO_KEEP variable
import java.util.ArrayList
def MAX_TO_KEEP = 10
def JOB_NAME = "job_name_here"
def hi = hudson.model.Hudson.instance
def item = hi.getItemByFullName(JOB_NAME)
def jobs = item.getAllJobs()
Iterator<?> iterator = jobs.iterator()
while (iterator.hasNext()) {
def job = iterator.next()
def recent = job.builds.limit(MAX_TO_KEEP)
for (build in job.builds) {
if (!recent.contains(build)) {
println "Preparing to delete: " + build
build.delete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment