Skip to content
Latchkey

git clean Command: Remove Untracked Files in CI

git clean removes untracked files (and optionally directories and ignored files) from the working tree.

On reused runners, leftover build artifacts from a previous job can poison the next one. git clean -fdx is the standard way to scrub the tree back to a pristine, tracked-only state.

Common flags

  • -f / --force - actually delete (required; clean refuses to run without it)
  • -d - also remove untracked directories
  • -x - also remove ignored files (build outputs, caches)
  • -X - remove only ignored files, keeping other untracked ones
  • -n / --dry-run - show what would be deleted without deleting
  • -e <pattern> - exclude paths from deletion

Example

shell
# Preview, then fully scrub the tree on a reused runner
git clean -ndx
git clean -fdx

In CI

git clean -fdx gives a pristine working tree on long-lived or warm runners, removing stale artifacts that cause flaky, hard-to-reproduce build failures. Always dry-run with -n first if a script is new, since -x also deletes ignored caches you may want to keep. Latchkey managed runners are ephemeral, so each job already starts clean.

Key takeaways

  • git clean -fdx removes untracked files, directories, and ignored artifacts in one pass.
  • -f is mandatory; clean refuses to delete without it.
  • Dry-run with -n before -x, which also wipes ignored caches.

Related guides

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