Skip to content
Latchkey

git clean -fdx: Usage, Options & Common CI Errors

git clean -fdx removes every untracked and ignored file and directory, leaving only tracked content.

When a reused runner workspace must be truly pristine, -fdx is the hammer. It is also a foot-gun: the -x flag deletes .gitignored caches, so use it deliberately.

What it does

git clean -fdx force-deletes untracked files (-f), descends into untracked directories (-d), and also removes ignored files (-x). The result is a tree containing only tracked, checked-out content.

Common usage

Terminal
git clean -fdxn               # DRY RUN first - list what dies
git clean -fdx
git clean -fdx -e .env        # but keep .env
git clean -fdx -e node_modules

Options

FlagWhat it does
-fActually delete (required)
-dRecurse into untracked directories
-xAlso remove ignored files
-n / --dry-runPreview without deleting
-e <pattern>Exclude paths from removal

Common errors in CI

There is no error to catch - that is the risk. -x removes restored caches like node_modules and build output, which can blow away a cache-restore step and slow the build. Always run -fdxn first, and use -e to protect anything a later step depends on.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →