Skip to content

Instantly share code, notes, and snippets.

@O5ten
Created September 18, 2018 14:22
Show Gist options
  • Save O5ten/a59cebf829d9890830e943293f4bcf37 to your computer and use it in GitHub Desktop.
Save O5ten/a59cebf829d9890830e943293f4bcf37 to your computer and use it in GitHub Desktop.
Clear Jenkins Sandbox Folder from jobs
import hudson.model.*
def isFolder = { it.class.canonicalName == 'com.cloudbees.hudson.plugins.folder.Folder' }
def isSandbox = { "sandbox" == it.fullName }
def deleteChildren = {
it.getItems().collect { item ->
def deletedName = item.fullName
item.delete()
"${deletedName} deleted"
}
}
def findAndClearSandbox = { allJobs ->
["\nJobs found and removed in sandbox",
"==================================",
allJobs.findAll(isFolder)
.findAll(isSandbox)
.collect(deleteChildren)
.flatten()
.join('\n') ?: 'Nobody has been using the sandbox this week. :(',
"=================================="].join('\n')
}
findAndClearSandbox(Hudson.instance.items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment