title: Jenkins Multibranch Pipeline: clean workspace / delete directory when build ends
body: <p>we are using Jenkins multibranch (scripted) pipeline to run multiple builds, in parallel. Each one will create a new, uniquely named directory to checkout the code and run the test. Directories are uniquely named because they contain the branch name but also a random part like "JAA2A5HUZB7TRE4OTVXU7S...".</p>
  
  <p>This means that after each build X directories (X = number of parallel jobs * number of branches) stay on the Jenkins node, filling disk space.</p>
  
  <p>I wonder how I could automatically delete these directories.</p>
  
  <p>A simplified version of my initial pipeline looks like this</p>
  
  <pre class="lang-groovy prettyprint-override"><code>// Initialize the matrix
  def matrix = [
    'foo',
    'bar',
  ]
  
  // Initialize empty tasks map
  def tasks = [:]
  
  // Job status
  successful = true
  
  // Fill our tasks map from the Matrix data
  for (x in matrix) {
  
    def job_name = x
  
    tasks[job_name] = {
      node('libvirt') {
          // Checkout repository first
          stage("$job_name - Checkout") {
            checkout scm
          }
  
          // Then build the machine
          gitlabCommitStatus('build') {
              stage("$job_name - Build") {
                    sh "./bin/build.sh ${job_name}"
              }
          }
        }
      }
    }
  }
  
  
  //// Pipeline ////
  
  notifyBuild('STARTED')
  
  // Run tasks in parallel
  try {
    parallel tasks
  } catch(e) {
    throw e
  } finally {
    if (successful) {
      notifyBuild('SUCCESS')
    } else {
      notifyBuild('FAILED')
    }
  }
  
  // Methods
  def notifyBuild(String buildStatus = 'STARTED') {
     // used to send formatted e-mails 
  }
  </code></pre>
  
  <p>I first added <code>deleteDir()</code> like this</p>
  
  <pre class="lang-groovy prettyprint-override"><code>} finally {
    if (successful) {
      notifyBuild('SUCCESS')
    } else {
      notifyBuild('FAILED')
    }
    node('libvirt') {
      deleteDir()
    }
  }
  </code></pre>
  
  <p>it raises an error and makes the build fail.</p>
  
  <p>I then added a <code>cleanWs()</code> like this</p>
  
  <pre class="lang-groovy prettyprint-override"><code>} finally {
    if (successful) {
      notifyBuild('SUCCESS')
    } else {
      notifyBuild('FAILED')
    }
    node('libvirt') {
      cleanWs()
    }
  }
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>Running on node in /srv/jenkins/workspace/pipeline-deletedir-DRD7EKBEMMWJQZW6KKMQVVBTJTPTKLRAE2ITDK7V7IB5PTFXZUZA
  [Pipeline] {
  [Pipeline] step
  [WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  </code></pre>
  
  <p>but the directory /srv/jenkins/workspace/pipeline-deletedir-DRD7EKBEMMWJQZW6KKMQVVBTJTPTKLRAE2ITDK7V7IB5PTFXZUZA still exists.</p>
  
  <p>I'm looking for any solution to adapt this scripted pipeline (and for declarative pipeline if you know how) to be able to delete all directories created by the build.</p>