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
continuous integration - Jenkins Pipeline Wipe Out Workspace - Stack Overflow
You can use deleteDir() as the last step of the pipeline Jenkinsfile (assuming you didn't change the working directory). ... 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. 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
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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) { sshPublisher( continueOnError: false, failOnError: true, publishers: [ sshPublisherDesc( ...
๐ŸŒ
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:
๐ŸŒ
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 - In this video, learn what gets tossed and what stays behind when you delete a view in comparison to deleting a folder.