Jenkins Pipeline Wipe Out Workspace

288,068

Solution 1

You can use deleteDir() as the last step of the pipeline Jenkinsfile (assuming you didn't change the working directory).

Solution 2

Like @gotgenes pointed out with Jenkins Version. 2.74, the below works, not sure since when, maybe if some one can edit and add the version above

cleanWs()

With, Jenkins Version 2.16 and the Workspace Cleanup Plugin, that I have, I use

step([$class: 'WsCleanup'])

to delete the workspace.

You can view it by going to

JENKINS_URL/job/<any Pipeline project>/pipeline-syntax

Then selecting "step: General Build Step" from Sample step and then selecting "Delete workspace when build is done" from Build step

Solution 3

The mentioned solutions deleteDir() and cleanWs() (if using the workspace cleanup plugin) both work, but the recommendation to use it in an extra build step is usually not the desired solution. If the build fails and the pipeline is aborted, this cleanup-stage is never reached and therefore the workspace is not cleaned on failed builds.

=> In most cases you should probably put it in a post-built-step condition like always:

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post { 
        always { 
            cleanWs()
        }
    }
}

Solution 4

In fact the deleteDir function recursively deletes the current directory and its contents. Symbolic links and junctions will not be followed but will be removed.

To delete a specific directory of a workspace wrap the deleteDir step in a dir step.

dir('directoryToDelete') {
    deleteDir()
}

Solution 5

Using the following pipeline script:

pipeline {
    agent { label "master" }
    options { skipDefaultCheckout() }
    stages {
        stage('CleanWorkspace') {
            steps {
                cleanWs()
            }
        }
    }
}

Follow these steps:

  1. Navigate to the latest build of the pipeline job you would like to clean the workspace of.
  2. Click the Replay link in the LHS menu.
  3. Paste the above script in the text box and click Run
Share:
288,068

Related videos on Youtube

qmo
Author by

qmo

Updated on December 02, 2021

Comments

  • qmo
    qmo over 2 years

    We are running Jenkins 2.x and love the new Pipeline plugin. However, with so many branches in a repository, disk space fills up quickly.

    Is there any plugin that's compatible with Pipeline that I can wipe out the workspace on a successful build?

  • ssindelar
    ssindelar almost 8 years
    I have problems with deleteDir(). It seems to randomly not able to delete the current directory when the node gets build on a slave. The build fails of course if this happens. So be aware if your jobs fail randomly. I don't understand why node not just cleans it workspace when the node starts building. Because the node can run anywhere you can't make any assumptions about the files in the workspace anyway.
  • Marcus Philip
    Marcus Philip over 7 years
    But I think will only delete the workspace on the current node. In the general case your pipeline will runs on several different slaves.
  • dsh
    dsh over 7 years
    This works for me: Jenkins 2.7.2, Workspace Cleanup Plugin 0.30
  • jpbochi
    jpbochi over 7 years
    I put this at the beggining right before checkout scm.
  • John McGehee
    John McGehee over 7 years
    Although the OP asked merely how to delete the workspace, this answer is the most informative.
  • davegallant
    davegallant over 7 years
    I put this at the beginning also, in case the project fails before it reaches the end, or the next build is on a different slave.
  • gotgenes
    gotgenes over 6 years
    According to this PR, included in 0.33, this is called in the pipeline as cleanWs.
  • Brandon
    Brandon over 6 years
    This was absolutely key for my use case. I need to archive artifacts from the job, and running cleanWs() as a step deletes them before the post build archive command runs. cleanWs() should most likely always be run as a post build command
  • vossad01
    vossad01 over 6 years
    This is the command documented to clean up the workspace in the "Cleaning up and notifications" section of the Jenkins documentation.
  • sirineBEJI
    sirineBEJI almost 6 years
    This does not work when having stages running on different slaves!
  • ᴠɪɴᴄᴇɴᴛ
    ᴠɪɴᴄᴇɴᴛ almost 6 years
    If you have only one post section, cleanWs() can safely be put into the always condition, but the safest spot is inside the cleanup condition: post { cleanup { cleanWs() } }
  • Aravind Murthy
    Aravind Murthy over 5 years
    We get an error "invalid condition cleanup", we are using Jenkins version 2.89
  • AkisK
    AkisK about 5 years
    Add options { skipDefaultCheckout() } for a little faster execution.
  • boweeb
    boweeb almost 5 years
    Beware deleteDir() won't work in a docker container. JENKINS-41894
  • mcw
    mcw almost 5 years
    Also works with a dockerized pipeline. Very helpful, thanks!
  • Andrew Gray
    Andrew Gray almost 5 years
    Improved answer with your suggestion @AkisK
  • Sergey Pleshakov
    Sergey Pleshakov almost 5 years
    Seems like this is the only option that works for cleaning up the workspace BEFORE and NOT AFTER executing a pipeline, even though I didn't want to have a separate step for just clean up. Thank you
  • David Lavender
    David Lavender over 4 years
    This is also the only answer I've seen that can kill the annoying @libs folder too
  • Denis Turgenev
    Denis Turgenev almost 3 years
    what is the difference between cleanWs and deleteDir? upvoted you
  • White
    White over 2 years
    In my opnion, this is one of the best answer, however, I would recommand to not delete the data in the failing case such as you are keeping debug data. Moreover, deleting the data at the beginning is also a good solution.
  • White
    White over 2 years
    what is the difference between cleanWs and deleteDir?
  • Pankaj Shinde
    Pankaj Shinde over 2 years