Skip to content

Instantly share code, notes, and snippets.

@prerak-proof
Created March 31, 2021 15:09
Show Gist options
  • Save prerak-proof/8ef4616cf90bb1f74f18a2814b3f4ea9 to your computer and use it in GitHub Desktop.
Save prerak-proof/8ef4616cf90bb1f74f18a2814b3f4ea9 to your computer and use it in GitHub Desktop.
Delete orphaned Jenkins workspaces
import jenkins.slaves.WorkspaceLocator
workspaceRoot = WorkspaceLocator.all()[0].getWorkspaceRoot(Jenkins.get())
processDir(workspaceRoot, Jenkins.instance)
def processDir(rootDir, holder) {
rootDir.list().each { dir ->
deleteIfInvalid(dir, holder.getItem(dir.name))
}
}
def deleteIfInvalid(dir, item) {
if (!item) {
println "Deleting [${dir}]"
dir.deleteRecursive()
} else if (item instanceof com.cloudbees.hudson.plugins.folder.Folder) {
processDir(dir, item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment