Jenkins Pipeline Wipe Out Workspace
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:
- Navigate to the latest build of the pipeline job you would like to clean the workspace of.
- Click the Replay link in the LHS menu.
- Paste the above script in the text box and click Run
Related videos on Youtube
qmo
Updated on December 02, 2021Comments
-
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 almost 8 yearsI 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 over 7 yearsBut I think will only delete the workspace on the current node. In the general case your pipeline will runs on several different slaves.
-
dsh over 7 yearsThis works for me: Jenkins 2.7.2, Workspace Cleanup Plugin 0.30
-
jpbochi over 7 yearsI put this at the beggining right before
checkout scm
. -
John McGehee over 7 yearsAlthough the OP asked merely how to delete the workspace, this answer is the most informative.
-
davegallant over 7 yearsI 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 over 6 years
-
Brandon over 6 yearsThis 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 over 6 yearsThis is the command documented to clean up the workspace in the "Cleaning up and notifications" section of the Jenkins documentation.
-
sirineBEJI almost 6 yearsThis does not work when having stages running on different slaves!
-
ᴠɪɴᴄᴇɴᴛ almost 6 yearsIf you have only one
post
section,cleanWs()
can safely be put into thealways
condition, but the safest spot is inside thecleanup
condition:post { cleanup { cleanWs() } }
-
Aravind Murthy over 5 yearsWe get an error "invalid condition cleanup", we are using Jenkins version 2.89
-
AkisK about 5 yearsAdd
options { skipDefaultCheckout() }
for a little faster execution. -
boweeb almost 5 yearsBeware
deleteDir()
won't work in a docker container. JENKINS-41894 -
mcw almost 5 yearsAlso works with a dockerized pipeline. Very helpful, thanks!
-
Andrew Gray almost 5 yearsImproved answer with your suggestion @AkisK
-
Sergey Pleshakov almost 5 yearsSeems 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 over 4 yearsThis is also the only answer I've seen that can kill the annoying @libs folder too
-
Denis Turgenev almost 3 yearswhat is the difference between cleanWs and deleteDir? upvoted you
-
White over 2 yearsIn 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 over 2 yearswhat is the difference between cleanWs and deleteDir?
-
Pankaj Shinde over 2 years