Skip to content

git clean -ndX: list every ignored build file across all your repositories, and the trap in it

One git command lists exactly what your .gitignore says is disposable. Run it across every repo to find build output, but read the list first: .env files are on it.

6 min read

Every repository already has a list of what is safe to throw away: its .gitignore. Build output, dependency folders and caches are on it because they can be regenerated. Git can list exactly those files, which makes it a precise way to find what is filling your code folder.

The command, as a dry run

cd ~/code/some-project
git clean -ndX
FlagMeaning
-nDry run. List, do not delete. Always start here
-dInclude whole directories, not only files
-X (capital)Only files your ignore rules match
-x (lower case)Ignored files and every untracked file. Far more dangerous

Across every repository

find ~/code -name .git -type d -prune 2>/dev/null | while read g; do
  r="${g%/.git}"
  n=$(git -C "$r" clean -ndX | wc -l | tr -d ' ')
  [ "$n" -gt 0 ] && printf '%6s  %s\n' "$n" "$r"
done | sort -n

That counts entries per repo. For sizes, size the listed paths in one repo:

cd ~/code/some-project
git clean -ndX | sed 's/^Would remove //' | tr '\n' '\0' | xargs -0 du -sh 2>/dev/null | sort -h | tail

The trap: what else is ignored

A .gitignore lists what should not be committed, which is not the same as what can be thrown away. In most projects the list includes:

  • .env and other secrets files. Ignored precisely because they are valuable. Losing one can mean regenerating API keys.
  • Local databases, such as SQLite files used in development.
  • Editor settings, such as .idea or .vscode folders with your run configurations.
  • Uploaded or generated data a project keeps outside version control.

So never run git clean -fdX across many repositories blind. Use the dry run to see what is large, and remove the build folders by name. Or use interactive mode in one repo, which lets you filter the list before anything is removed:

git clean -idX

What usually turns up

node_modules, target for Rust, .next and dist for web projects, build for many others, Python's __pycache__ and virtual environments. Our guides to node_modules, frontend build caches and the developer cleanup script cover each in more depth. For repositories that are themselves the problem, see Git repositories taking up space.

Common questions

What does git clean -ndX do?

It lists, without deleting, every file and folder in the repository that matches the ignore rules. -n is a dry run, -d includes directories, and capital -X limits it to ignored files.

What is the difference between git clean -x and -X?

Capital -X removes only files matched by .gitignore. Lower case -x removes those and every untracked file as well, including new work you have not committed.

Is it safe to delete all gitignored files?

Not blindly. .env files, local databases and editor settings are usually ignored too. Review the dry run and remove build folders by name, or use git clean -idX to filter interactively.

Read next