Jenkins Pipeline Wipe Out Workspace Jenkins Pipeline Wipe Out Workspace jenkins jenkins

Jenkins Pipeline Wipe Out Workspace


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


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


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()        }    }}