How to remove untracked files in Git?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Untracked files are files in your working tree that Git sees but is not currently tracking. They are often harmless build outputs, generated artifacts, scratch notes, or temporary files, but they can clutter git status and cause confusion if you are trying to inspect a clean working directory.
Start by seeing what would be removed
The main command for deleting untracked files is git clean, but it is intentionally conservative because it can permanently delete data. The safest first step is always a dry run.
The -n flag means "show me what would happen, but do not actually delete anything." In most cases, this should be your first command.
If you also want to preview untracked directories, add -d.
Remove untracked files
Once the dry run output looks correct, remove untracked files with -f.
Git requires -f because deleting untracked files is destructive. Without it, the command usually refuses to run.
To remove untracked directories too, use:
That is the common cleanup command in repositories that generate temporary directories during builds.
Ignored files versus untracked files
Ignored files are not the same as untracked files. An ignored file matches rules in .gitignore, .git/info/exclude, or a global ignore file.
If you want to remove only ignored files, use -X.
If you want to remove both ignored and untracked files, use lowercase -x.
That last form is powerful and risky. It can wipe build artifacts, local environment files, and anything else Git is not tracking.
Interactive cleanup
If you want more control, Git also supports interactive mode.
Interactive mode lets you review candidates, filter them, and confirm deletions in steps. It is slower than a direct command, but it is safer when you are unsure.
Typical workflow
A good cleanup workflow looks like this:
- inspect the status
- preview cleanup with
git clean -nd - run the destructive form only after checking the list
That sequence reduces the chance of deleting a file you actually meant to keep.
When not to use git clean
Do not use git clean as a reflex whenever your working tree looks messy. If a file is important but untracked, git clean will not warn you that it contains valuable work.
It is also the wrong tool for deleting tracked changes. For tracked files modified in the working tree, you need a different Git command, and that distinction matters because tracked and untracked state are handled differently.
Common Pitfalls
The biggest mistake is running git clean -fdx without a dry run. That command is useful, but it is also the fastest way to delete local files you forgot were untracked.
Another common issue is assuming git clean affects tracked files. It does not. If the file is already tracked by Git, git clean will leave it alone.
A third problem is forgetting about ignored files. Developers often expect git clean -fd to remove everything generated by the build, but ignored files remain unless you use -X or -x.
Summary
- Use
git cleanto remove untracked files from the working tree. - Start with
git clean -norgit clean -ndto preview the deletion set. - Use
git clean -ffor files andgit clean -fdfor files plus directories. - Use
-Xfor ignored files only and-xfor ignored plus untracked files. - Treat
git cleanas destructive and verify the preview before deleting anything.

