Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save indrgun/4312429bdfbbef067740036666743f55 to your computer and use it in GitHub Desktop.

Select an option

Save indrgun/4312429bdfbbef067740036666743f55 to your computer and use it in GitHub Desktop.

Revisions

  1. @ceilfors ceilfors revised this gist Sep 9, 2015. No changes.
  2. @ceilfors ceilfors revised this gist Aug 27, 2015. No changes.
  3. @ceilfors ceilfors revised this gist Aug 27, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions cleanupUnusedWorkspaceInSlaves.groovy
    Original file line number Diff line number Diff line change
    @@ -10,19 +10,19 @@ def boolean isFolder(String name) {
    def deleteUnusedWorkspace(FilePath root, String path) {
    root.list().each { child ->
    String fullName = path + child.name
    if (isFolder(path + child.name)) {
    deleteUnusedWorkspace(root.child(child.name), path + "$child.name/")
    if (isFolder(fullName)) {
    deleteUnusedWorkspace(root.child(child.name), "$fullName/")
    } else {
    if (Jenkins.instance.getItemByFullName(fullName) == null) {
    println "Deleting unused workspace $fullName "
    println "Deleting: $fullName "
    child.deleteRecursive()
    }
    }
    }
    }

    for (node in Jenkins.instance.nodes) {
    println node.displayName
    println "Processing $node.displayName"
    def workspaceRoot = node.rootPath.child("workspace");
    deleteUnusedWorkspace(workspaceRoot, "")
    }
  4. @ceilfors ceilfors created this gist Aug 27, 2015.
    28 changes: 28 additions & 0 deletions cleanupUnusedWorkspaceInSlaves.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import com.cloudbees.hudson.plugins.folder.Folder
    import hudson.FilePath
    import jenkins.model.Jenkins

    def boolean isFolder(String name) {
    def item = Jenkins.instance.getItemByFullName(name)
    return item instanceof Folder
    }

    def deleteUnusedWorkspace(FilePath root, String path) {
    root.list().each { child ->
    String fullName = path + child.name
    if (isFolder(path + child.name)) {
    deleteUnusedWorkspace(root.child(child.name), path + "$child.name/")
    } else {
    if (Jenkins.instance.getItemByFullName(fullName) == null) {
    println "Deleting unused workspace $fullName "
    child.deleteRecursive()
    }
    }
    }
    }

    for (node in Jenkins.instance.nodes) {
    println node.displayName
    def workspaceRoot = node.rootPath.child("workspace");
    deleteUnusedWorkspace(workspaceRoot, "")
    }