Skip to content
Latchkey

Jenkins cleanWs Failed - Fix Workspace Cleanup Errors

The cleanWs() step (Workspace Cleanup plugin) could not clear the workspace. Files are locked by a running process or were created root-owned by a container step, the plugin is not installed, or the cleanup ran where the files are not.

What this error means

A cleanWs() step warns or fails - Cannot delete a path, a permission error, or No such DSL method 'cleanWs' if the plugin is absent. The workspace is left partly populated and the next run may inherit stale or root-owned files.

Jenkins console
[WS-CLEANUP] Deleting project workspace...
ERROR: Cannot delete workspace: /home/jenkins/workspace/app/build/out:
Permission denied
# or, plugin missing:
No such DSL method 'cleanWs' found among steps [...]

Common causes

Root-owned or locked files block deletion

A container step that wrote as UID 0, or a process still holding files open, leaves paths the agent user cannot delete during cleanup.

Plugin missing or cleanup on the wrong node

Without the Workspace Cleanup plugin, cleanWs is unknown. Running it on a different agent/dir than the build leaves the real workspace untouched.

How to fix it

Run containers as the agent user and clean in post

Match container UID to the agent so files stay deletable, and clean up in a post { cleanup } block on the same node.

Jenkinsfile
pipeline {
  agent { label 'linux' }
  stages {
    stage('Build') {
      steps { sh 'docker run --rm -u 1000:1000 -v $PWD:/w -w /w node:20 npm ci' }
    }
  }
  post { cleanup { cleanWs() } }
}

Force-clean stubborn paths

  1. Install the Workspace Cleanup plugin if cleanWs is unknown.
  2. Reset ownership of stray root-owned files before cleanup (run a container as root to chown/rm them).
  3. Run cleanWs() on the same agent that holds the workspace, in a post block.

How to prevent it

  • Run workspace-touching containers with the agent UID/GID.
  • Clean the workspace in post { cleanup } on the build’s own node.
  • Keep the Workspace Cleanup plugin installed where pipelines call cleanWs.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →