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 โ€บ 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?

๐ŸŒ
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:
๐ŸŒ
LinkedIn
linkedin.com โ€บ learning โ€บ learning-jenkins-14423877 โ€บ delete-views-and-folders
Delete views and folders - Jenkins Video Tutorial | LinkedIn Learning, formerly Lynda.com
December 8, 2021 - - [Instructor] So now that we've moved all the jobs into folders specific to the team that we're developing mobile apps for. Things are looking pretty organized in the Jenkins Interface. But before we move on, I wanted to give some emphasis on what happens if you delete a folder or a view.