I just figured it out:
bat rmdir \"target/generated-sources/something\" /S /Q
There was an additional catch, the bat or cmd or whatever exe is executing the delete operation is refusing to delete the folder if it is not empty. Some MSDN site said you can use /S to delete the entire tree directory tree including the stated folder and the /Q turns it quiet i.e. forces the operation (like -f in PowerShell)
Answer from DanDan on Stack OverflowHow can I delete files in directory using jenkins - Stack Overflow
Jenkins Multibranch Pipeline: clean workspace / delete directory when build ends - Stack Overflow
linux - Jenkins pipeline - function to remove properly a folder - Stack Overflow
continuous integration - Jenkins Pipeline Wipe Out Workspace - Stack Overflow
Videos
I just figured it out:
bat rmdir \"target/generated-sources/something\" /S /Q
There was an additional catch, the bat or cmd or whatever exe is executing the delete operation is refusing to delete the folder if it is not empty. Some MSDN site said you can use /S to delete the entire tree directory tree including the stated folder and the /Q turns it quiet i.e. forces the operation (like -f in PowerShell)
Only commands in dir(){} block are executed in that dir, so there is no need to navigate back explicitly.
You can delete the whole folder using this syntax to delete a folder called bin:
stage('Setup') {
steps {
dir ('bin') {
deleteDir()
}
}
}
If my understanding is right, consider my below assumptions
If your jenkins is running on a Unix server, then you can configure a post build step as suggested by nerdwaller above
- In the job configuration, in the build step, select the option "execute unix command"
- In the box for the shell script, you can use
rm -rf <<directoryname>>
Else, if your jenkins is running on a windows server, then select "execute batch command" from the build step and give the appropriate command like rmdir /Q /S nonemptydir
However, my best approach would be to use a platform independent tool like ant to delete the folders using Ant Delete Task and it can be configured similarly like the above two approaches instead selecting "invoke ant" in the build step/ post build step.
This will help you to achieve what you need.
The solution has been to add a try/catch/finally in the stage("$job_name - Build") step, something like
gitlabCommitStatus('build') {
stage("$job_name - Build") {
try {
sh "./bin/build.sh ${job_name}"
} catch (Exception e) {
raise e
} finally {
deleteDir()
}
}
}
This doesn't delete all directories involved (because of this Jenkins issue https://issues.jenkins-ci.org/browse/JENKINS-41805) but it remove all the voluminous directories (containing code/build/artifacts...).
You needs to add "raise e" or build will always return Success, even if one of them fails.
First of all, you can not delete the directory in which the build is still running. The deleteDir() step just deletes the content of your workspace.
You could trigger a job that runs after your build is finished which deletes those directories. For this, you could use a pattern like pipeline-deletedir-*.
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
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()
}
}
}
I used customWorkspace in Jenkins then deleteDir(), but it doesn't delete the @tmp folder.
So to delete @tmp along with the workspace, use the following:
pipeline {
agent {
node {
customWorkspace "/home/jenkins/jenkins_workspace/${JOB_NAME}_${BUILD_NUMBER}"
}
}
post {
cleanup {
/* clean up our workspace */
deleteDir()
/* clean up tmp directory */
dir("${workspace}@tmp") {
deleteDir()
}
/* clean up script directory */
dir("${workspace}@script") {
deleteDir()
}
}
}
}
This snippet will work for default workspace also.
Not a direct answer to your question, but might help someone.
If you are using the dir Jenkins pipeline step to change directories, then you'll see these @tmp folders getting created. Instead, use the cd command and they won't get created in the first place.
So, instead of,
def myFunc(String folder) {
dir(folder) {
// work
}
}
Do this.
def myFunc(String folder) {
bat "cd ${folder}"
// work
}
This is about files in `/var/lib/jenkins/workspace`, not `/var/lib/jenkins/jobs`
In a declarative Jenkinsfile for a multi-branch pipeline you can declare a `buildDiscarder` that removes build logs and artifacts after a certain number of days.
I have set up a Bitbucket multi-branch pipeline which correctly triggers on opening Pull Requests. As far as I can see it also marks "projects" (i.e. pull request builds) as "disabled" when the PR is merged to master (or declined). I can't see where it does this or allows setting it, but it appears to do so.
However the directories of the format: `/var/lib/jenkins/workspace/my-project_PR-1234@script` for that PR remain afterwards. Despite requesting shallow copies these directories seem to total around 0.5GB per branch, meaning after lots of open PRs we're using lots of HDD space for workspaces that will never be used.
Is there a way to (in order of best to least good, but likely most complex to most blunt):
-
Have whatever process "disables" a branch also delete the workspace?
-
Delete workspaces that haven't had a build in X days?
-
Just generally wipe workspaces older than X days?