Jenkins Pipeline - Clean before checkout - git clean -fdx "failed to remove: Directory not empty" I have my Jenkins build configured to "Clean before Checkout" for its git source:

Job Config &gt; Source &gt; git &gt; Behaviours &gt; Clean before checkout


This results in

Resetting working tree

git reset --hard # timeout=10
git clean -fdx # timeout=10


Before the job begins.

This errors out with

stderr: warning: failed to remove web/static/node_modules/.../fonts/google-fonts: Directory not empty


If I re-run the job (simply replay), I get the same error.

If I re-run it again, git clean works fine and the job finishes with success in the end.

If I start it again it errors out 2 times again until it works at the third time. This is always the case.

What might be going on here?
We didn't find the cause, neither a real solution.

There is a workaround though:

Put a pipeline stage with rm -rf * before your scm checkout stage. This solves the problem but I will change the accepted answer to whoever posts the real reason and fix for the problem.

Sidenote:

Keep in mind that you will erase anything in your Jenkins workspace doing that. So be certain that you have everything you need in Git so it comes back with the checkout or get it back some other way.

If you rely on files in your Jenkins workspace from previous runs then this method is not for you!

(Although you might want to think about adapting your Job so files from previous runs are not needed anymore. Every run should contain the files it needs by itself and not rely on other runs.)