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 Overflow
🌐
Debug This
debugthis.dev › jenkins › 2020-01-22-jenkins-pipeline-deleting-temporary-workspace-files
Deleting temporary workspace files within a Jenkins pipeline
January 22, 2020 - Using deleteDir() from within a pipeline stage does not delete the @tmp, @script directories which get generated at run-time. Over time, this leads to Jenkins pipelines taking up disk space across Jenkins slave nodes or worse case directly on the master; whether this is checked out code, built binaries etc.. To free up disk space, add the following to the post stage of a pipeline. Here we are explicitly specifying the dir() with the workspace path to remove recursively.
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-basic-steps
Pipeline: Basic Steps
Recursively deletes the current directory and its contents. Symbolic links and junctions will not be followed but will be removed.
Discussions

How can I delete files in directory using jenkins - Stack Overflow
This is a great solution as it's ... it, so it never fails or throw any "Directory doesn't exist" errors! 2019-02-04T17:05:23.88Z+00:00 ... @nicolas-castro , this is to be inserted inside the Jenkinsfile (i.e., the pipeline code).... More on stackoverflow.com
🌐 stackoverflow.com
Jenkins Multibranch Pipeline: clean workspace / delete directory when build ends - Stack Overflow
Well sorry that wasn't much help, I do know that the build discarder is removing the job directories on the master. Also as an aside, I actually do my deleteDir() in some instances at the beginning of the pipeline. I wish I could point to docs (hard to find stuff sometimes with Jenkins), but I ... More on stackoverflow.com
🌐 stackoverflow.com
linux - Jenkins pipeline - function to remove properly a folder - Stack Overflow
I need to implement a function in groovy (for a Jenkins pipeline) which will remove a specific folder into a linux server. def remove_folder(String path_of_the_folder) { sh "rm -rf More on stackoverflow.com
🌐 stackoverflow.com
February 4, 2022
continuous integration - Jenkins Pipeline Wipe Out Workspace - Stack Overflow
Currently both deletedir() and ... using Jenkins kubernetes plugin, the pod workspace is deleted but the master workspace persists · it should not be a problem for persistant branches, when you have a step to clean the workspace prior to checkout scam. It will basically reuse the same workspace over and over again: but when using multibranch pipelines the master keeps the whole workspace and git directory... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-41805
Pipeline Job-- deletedir() delete only current directory but ...
February 24, 2021 - Directories with an ampersand (like @tmp and @script, @libs) are not removed when using 'deletedir()' in pipeline stage.
🌐
Stack Overflow
stackoverflow.com › questions › 70985873 › jenkins-pipeline-function-to-remove-properly-a-folder
linux - Jenkins pipeline - function to remove properly a folder - Stack Overflow
February 4, 2022 - See Pipeline variables. Or just test the variable is set and not root before proceeding. ... Hello How I can be sure that is not the root folder ? That root_path/path_of_the_folder is not root_path eventually ? ... [[ ${ROOT_PATH} != "/" && -d ${ROOT_PATH}/${path_of_the_folder} ]] && rm -fR ${ROOT_PATH}/${path_of_the_folder} ... Jenkins ver.
Find elsewhere
🌐
GitHub
github.com › manheim › jenkinsfile-pipeline › blob › master › docs › DeleteDirPlugin.md
jenkinsfile-pipeline/docs/DeleteDirPlugin.md at master · manheim/jenkinsfile-pipeline
Enable this plugin to run deleteDir between Stages, and recursively delete the current directory from the workspace. @Library('jenkinsfile-pipeline@<VERSION>') _ Jenkinsfile.init(this) // Call `deleteDir` and resurively delete the current directory ...
Author   manheim
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-41805
Loading...
December 7, 2022 - Directories with an ampersand (like @tmp and @script, @libs) are not removed when using 'deletedir()' in pipeline stage.
🌐
Jenkins
community.jenkins.io › t › delete-jenkins-empty-folder-and-pipelines › 19741
Delete Jenkins Empty folder and Pipelines - Community - Jenkins
September 18, 2024 - I need a help to delete Jenkins Pipeline job and empty folder using python script pipeline
🌐
Jenkins
community.jenkins.io › using jenkins
Need command to delete particular folder before deploying - Using Jenkins - Jenkins
April 27, 2023 - Hi Team After jenkins build ,I am trying to push build folder from jenkins to destination uisng below pipeline command. stage(‘Upload to server’) { def deployStatus = “” try { ws(this.WORKSPACE_PATH) { …
🌐
Google Groups
groups.google.com › g › jenkinsci-users › c › lQgJLVnIAaU
Jenkins pipeline - Remove @tmp folder
April 30, 2016 - There is a way to tell the pipeline to remove that folder after it finishes? ... As far as I read seems the 'sh' command creates the directory, but I want to remove it after it finishes or to set a different tmp folder path.
🌐
Jenkins
plugins.jenkins.io › ws-cleanup
Workspace Cleanup | Jenkins plugin
August 11, 2025 - When deferred wipeout is disabled, the old implementation of filesystem content deletion is used. If you want the same behavior as with deferred wipeout, you have to set the plugin attribute deleteDirs to true as well. For Pipeline jobs, you can do this as follows:
🌐
Reddit
reddit.com › r/jenkinsci › auto-deleting workspace directories
r/jenkinsci on Reddit: Auto-deleting workspace directories
March 23, 2020 -

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?

🌐
Reddit
reddit.com › r/jenkinsci › cleaning up workspaces
r/jenkinsci on Reddit: Cleaning Up Workspaces
June 28, 2019 -

I was wondering how everyone deals with workspaces. I administer my company's Jenkins instances, but I do not own the individual jobs and have no control over their configurations. The vast majority of the jobs are configured to check out a fresh copy from SVN each time, but are not configured to delete their workspaces after. So eventually the Jenkins workspace folder grows very large. There isn't any benefit to keeping the files, since they will be re-checked out for the next build anyway.

Does anyone have a set process to deal with the workspace directory or a solution for managing it? I saw the External Workspace Manager plugin, but it appears to be much more geared to archiving the workspaces for examination, rather than deleting them.

🌐
Jenkins-ci
wiki.jenkins-ci.org › JENKINS › Workspace-Cleanup-Plugin.html
Jenkins : Workspace Cleanup Plugin
When deferred wipeout is disabled the old implementation of filesystem content deletion is used. If you want the same behavior as with deferred wipeout, you have to set plugin attribute deleteDirs to true as well. In pipeline job's script, you can do that like: