Skip to end of metadata
Go to start of metadata

Useful before you make a copy or backup of your Jenkins installation.

This script wipes out the workspaces of all jobs on your Jenkins server

import hudson.model.*
// For each project
for(item in Hudson.instance.items) {
  // check that job is not building
  if(!item.isBuilding()) {
    println("Wiping out workspace of job "+item.name)
    item.doDoWipeOutWorkspace()
  }
  else {
    println("Skipping job "+item.name+", currently building")
  }
}
  • No labels

3 Comments

  1. If you have the CloudBeesFolder Plugin installed you may use this script:

    import hudson.model.*
    
    jenkins.model.Jenkins.instance.items.each {
      job ->
        // if this is a special cloudbees job
        if (job.class.canonicalName.equalsIgnoreCase("com.cloudbees.hudson.plugins.folder.Folder")) {
          com.cloudbees.hudson.plugins.folder.Folder myJob = (com.cloudbees.hudson.plugins.folder.Folder) job
          myJob.items.each {
            item ->
              updateJob(item)
          }
        } else {
          updateJob(job)
        }
    }
    
    private updateJob(TopLevelItem item) {
      // check that job is not building
      if(!item.isBuilding()) {
        println("Wiping out workspace of job "+item.name)
        item.doDoWipeOutWorkspace()
      }
      else {
        println("Skipping job "+item.name+", currently building")
      }
    }
    
  2. The above may not support multiple levels of folder or something, didn't investigate. The following code works for me.

    import hudson.model.*
    
    deleteWorkspace (Hudson.instance.items) 
    
    def deleteWorkspace (items) {
      for (item in items) {
        if (item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder') {
            if(!item.isBuilding()) {
                println("Wiping out workspace of job "+item.name)
                item.doDoWipeOutWorkspace()
            } else {
                println("Skipping job "+item.name+", currently building")
            } 
       } else {
            deleteWorkspace(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems())
       }
     }
    }
    
  3. With some of the new job types, the above code will crash as they either cannot check if they're being build or do not have workspaces.

    So a slight updated version would be something like this:

    import hudson.model.*
    
    deleteWorkspace (Hudson.instance.items)
    
    def deleteWorkspace (items) {
      for (item in items) {
        if (item.class.canonicalName != null
    		&& item.class.canonicalName != "com.cloudbees.hudson.plugins.folder.Folder"
            	&& item.class.canonicalName != "org.jenkinsci.plugins.workflow.job.WorkflowJob"
           		&& item.class.canonicalName != "com.github.mjdetullio.jenkins.plugins.multibranch.MavenMultiBranchProject"
           		&& item.class.canonicalName != "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject"
           		&& item.class.canonicalName != "hudson.model.ExternalJob") {
          	println("Item of type "+item.class.canonicalName+" found")
            if(!item.isBuilding()) {
                println("Wiping out workspace of job "+item.name)
                item.doDoWipeOutWorkspace()
            } else {
                println("Skipping job "+item.name+", currently building")
            }
       } else if (item.class.canonicalName == "com.cloudbees.hudson.plugins.folder.Folder") {
          	println("Item is folder with name "+item.name)
            deleteWorkspace(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems())
        } else {
          	println("Item of type "+item.class.canonicalName + " cannot have its workspace cleaned")
        }
     }
    }