Skip to content

Instantly share code, notes, and snippets.

@jbasila
jbasila / jenkins-delete-builds.groovy
Last active September 16, 2019 12:00
Jenkins: Delete older builds
// run this on the master:
// $ du -sk MAG-* |sort -rn|head -20 | while read _line ; do _ar=( ${_line} ); echo -n "\"$(cat ${_ar[1]}/name-utf8.txt)\", "; done ;echo ""
_array=[ ]
_array.each {
JOB_NAME="Magnifier/${it}"
KEEP_LAST_NUM_OF_BUILDS=2
Jenkins.instance.getItemByFullName("${JOB_NAME}").builds.findAll { true }.each {
if (KEEP_LAST_NUM_OF_BUILDS) {
println( hudson.util.Secret.decrypt("${ENCRYPTED_PASSPHRASE_OR_PASSWORD}") )
def jobname = "the-job-name"
def buildnum = 85
def job = Jenkins.instance.getItemByFullName(jobname)
for (build in job.builds) {
if (buildnum == build.getNumber().toInteger()){
if (build.isBuilding()){
build.doStop();
build.doKill();