How can I clean the workspace in Windows agents?

Article ID:360032353851
1 minute readKnowledge base

Issue

We are having problems cleaning up the build workspace when using Git plugin in Windows nodes, this is happening the second time that we run the job on the same agent without restarting it.

[WS-CLEANUP] Deleting project workspace...
ERROR: [WS-CLEANUP] Cannot delete workspace: remote file operation faileX: X:\Folder\workspace\XXX\XXX at hudson.remoting.Channel@xxxxx:JNLP4-connect connection from XXX: java.nio.file.AccessDeniedException: X:\Folder\workspace\XXX\XXX -> X:\Folder\workspace\XXX\XXX_ws-cleanup_xxxx
ERROR: Cannot delete workspace: remote file operation faileX: X:\Folder\workspace\XXX\XXX at hudson.remoting.Channel@xxxxx:JNLP4-connect connection from XXX: java.nio.file.AccessDeniedException: X:\Folder\workspace\XXX\XXX -> X:\Folder\workspace\XXX\XXX_ws-cleanup_xxxx

This will not work even if you exclude the files reported as blocked using excusions. image::common-kb::CE-3209-001.png[]

This kind of configuration will result on an error like the one shown below:

Wiping out workspace first.
java.nio.file.FileSystemException: X:\Folder\workspace\XXX\XXX\.git\objects\pack\pack-xxxxxxxxxxx.pack: The process cannot access the file because it is being used by another process.

Resolution

You need to add an additional behavior in the Git Plugin section that performs a Clean before checkout operation as shown below: image::common-kb::CE-3209-002.png[]

Workaround