Cleaning up Jenkins workspace in the node

The pipelines I setup are all running on the node. I’d like to cleanup the workspace post build. I already installed workspace cleanup plug-in and tried cleanws() but it doesn’t seem to work.

Hi there,

“it doesn’t work” is always hard to remote debug. Try to always include these 3 things.

  1. what did you try? (Code samples, command lines, screenshots, videos, etc)
  2. what did happen? (Error messages, description, outputs, stuff)
  3. what did you expect to happen?

Otherwise, it’s hard for us to get into your context and you’ll have to have someone who is exactly knowledgeable come along.

what did you try? (Code samples, command lines, screenshots, videos, etc)
I just called cleanws() in the jenkinsfile in the build stage
and in the post build stage cleanWs deleteDirs:…

what did happen? (Error messages, description, outputs, stuff)
No errors.

what did you expect to happen?
I expect it to cleanup the workspace dir under /home/user/jenkins/workspace/myapp_pipeline

I can’t duplicate the behavior that you’re describing.

I ran the following Pipeline script with Jenkins 2.401.1 and the plugin releases as documented in my plugins.txt file and can see that cleanWS() removes the contents of the workspace.

pipeline {
    agent any

    stages {
        stage('test cleanWs') {
            steps {
                echo 'Hello World'
                writeFile file: 'file-name.txt', text: "Content in file-name.txt file from build ${BUILD_ID}"
                script {
                    content = readFile 'file-name.txt'
                    echo "File content is ${content}"
                }
                cleanWs()
                script {
                    content = readFile 'file-name.txt'
                    echo "File content is ${content}"
                }
            }
        }
    }
}

The second block that tries to read the contents of the file after cleanWS() fails as expected because the file does not exist.

Thanks. I will try and let you know.