How to Remove Untracked Files in Git

Files in the Git working directory can be either tracked or untracked. Tracked files are the ones that have been added and committed — Git knows about them, and they can be unmodified, modified, or staged. All other files in the working directory are untracked.
Over time, your working directory can get cluttered with auto-generated files, merge leftovers, or files created by mistake. You can either add those files to .gitignore
or remove them. If you want to keep your repository clean, removing the unnecessary files is the better option.
This guide explains how to remove untracked files and directories in Git using the git clean command.
Checking Untracked Files
Before removing anything, run git status to see which files are untracked:
git statusUntracked files appear under the “Untracked files” section in the output.
Using git clean
The git clean command removes untracked files from the working directory. The general syntax is:
git clean [OPTIONS] [PATH]git clean permanently deletes files. Once removed, untracked files cannot be recovered because they were never committed. Always run a dry run first.Dry Run
Before running the actual command, use the -n option to perform a dry run. This shows what files and directories would be deleted without actually removing them:
git clean -dnWould remove content/test/
Would remove content/blog/post/example.mdIf any of the listed files are important, either start tracking them with git add <file> or add them to your .gitignore.
Removing Files and Directories
Once you are sure you want to delete the untracked files, run:
git clean -dfRemoving content/test/
Removing content/blog/post/example.mdThe options used are:
-d— Remove untracked directories in addition to files-f— Force the removal. Git requires this flag by default because theclean.requireForceconfiguration variable is set totrue. Without-f, Git will refuse to delete anything
If you do not want to remove empty untracked directories, omit the -d option.
Interactive Mode
To interactively choose which files to delete, use the -i option:
git clean -diWould remove the following items:
content/test/ content/blog/post/example.md
*** Commands ***
1: clean 2: filter by pattern 3: select by numbers
4: ask each 5: quit 6: helpSelect one of the choices and press Enter. The “ask each” option is useful when you want to review files one by one before deleting them.
Limiting to a Specific Path
You can limit the clean operation to a specific directory by passing the path as an argument. For example, to check for untracked files only under the src directory:
git clean -dn src/To remove them:
git clean -df src/Removing Ignored Files
The git clean command can also remove files that are listed in .gitignore, such as build artifacts, compiled files, or dependency directories.
To remove all untracked and ignored files and directories, use the -x option:
git clean -dfxTo remove only the ignored files and directories while keeping regular untracked files, use the uppercase -X option:
git clean -dfXThis is useful for cleaning build output without affecting new source files you have not yet tracked.
git stash --include-untracked
. This stashes both staged changes and untracked files, and you can restore them later with git stash pop.Troubleshooting
fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given
Git requires explicit confirmation for destructive cleanup. Run a preview with git clean -dn, then run the actual cleanup with git clean -df.
git clean removed files you wanted to keep
Before cleanup, stage important files with git add <file> or stash them with git stash --include-untracked. After cleanup, Git cannot restore files that were never committed.
Ignored files are still present after cleanup
Use the right flag for your goal: -x removes untracked and ignored files, while -X removes only ignored files.
Quick Reference
| Command | Description |
|---|---|
git clean -dn | Dry run — show what would be removed |
git clean -df | Remove untracked files and directories |
git clean -di | Interactive mode — choose what to remove |
git clean -dfx | Remove untracked and ignored files |
git clean -dfX | Remove only ignored files |
git clean -df src/ | Remove untracked files in a specific path |
FAQ
What is the difference between -x and -X in git clean?
Lowercase -x removes both untracked and ignored files. Uppercase -X removes only ignored files (those matched by .gitignore) while keeping regular untracked files.
Can I recover files deleted by git clean?
No. Because untracked files were never committed, Git has no record of them. Always run git clean -dn first to preview what will be deleted.
Why does git clean say “refusing to clean”?
Git requires the -f (force) flag by default. This is controlled by the clean.requireForce configuration variable, which is set to true in most Git installations to prevent accidental file deletion.
Conclusion
The git clean command removes untracked files and directories from your working directory. Always run a dry run with -dn before using -df to avoid accidentally deleting important files. Use -X to clean only ignored build artifacts, or -x to remove everything Git does not track.
If you have any questions, feel free to leave a comment below.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page