Skip to content

Instantly share code, notes, and snippets.

@mirzmaster
Created May 6, 2019 18:32
Show Gist options
  • Save mirzmaster/cdc33532cd67c43d01b22ae2c90b4613 to your computer and use it in GitHub Desktop.
Save mirzmaster/cdc33532cd67c43d01b22ae2c90b4613 to your computer and use it in GitHub Desktop.
Jenkins Groovy script to wipe all workspaces from disk
import com.cloudbees.hudson.plugins.folder.*
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
import org.jenkinsci.plugins.workflow.job.WorkflowJob
for (item in Hudson.instance.items) {
wipeWorkspace(item)
}
def wipeWorkspace(item) {
println("--> found " + item.getClass().getCanonicalName())
if (item instanceof Folder) {
println(" -> entering folder: " + item.name)
for (innerItem in item.getItems()) {
wipeWorkspace(innerItem)
}
} else if (item instanceof Job) {
if (!item.isBuilding()) {
println(" wiping workspace: " + item.name)
item.doDoWipeOutWorkspace()
} else {
println(" skipping: " + item.name + " (build in progress)")
}
} else if (item instanceof WorkflowJob || item instanceof WorkflowMultiBranchProject) {
// can't handle workflow (i.e. pipeline) jobs
println(" skipping: " + item.name + " (workflow job)")
} else {
println(" unsupported job type: " + item.name + " (" + item.getClass().getCanonicalName() + ")")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment