Git Clean: Safely Remove Untracked Files & Folders (Complete Guide for 2026)

Published February 16, 2026 · 10 min read

git clean is the command you use when your working tree is full of untracked junk: build artifacts, temporary files, generated folders, and leftovers from switching branches.

It’s also a command that can delete things permanently if you run it carelessly.

Important: git clean deletes files from disk. Git cannot restore them because they were never committed. Always dry-run first: git clean -n (and git clean -nd for directories).
⚙ Quick links: Git Undo (Reset vs Revert vs Restore) · Git Restore (discard/unstage) · Git Stash (save WIP) · Git Reflog Recovery · Git Commands Cheat Sheet
Quick decision: If you want to delete untracked files, use git clean. If you want to discard edits to a tracked file, use git restore. If you want to recover “lost commits”, use git reflog.

Table of contents

  1. Copy/paste recipes
  2. What git clean deletes (and what it won’t)
  3. The safe workflow (status → dry-run → delete)
  4. Flags you actually need (-n, -f, -d, -x, -X, -i)
  5. Cleaning ignored files (node_modules, dist, target)
  6. Interactive cleaning (git clean -di)
  7. Excluding files and folders (-e)
  8. Common real-world use cases
  9. Cleaning submodules
  10. Troubleshooting
  11. FAQ

1. Copy/paste recipes (the 90% cases)

Preview what would be deleted (recommended)

# Dry run: prints what would be removed
git clean -n

# Dry run, including untracked directories
git clean -nd

Delete untracked files (not directories)

# Remove untracked files in the current repo
git clean -f

Delete untracked files and directories

# Common “clean everything untracked” command
git clean -fd

Delete only ignored files (keep other untracked files)

# Remove ignored files only (build output, node_modules if ignored)
git clean -fX

# Include ignored directories too
git clean -fdX

Delete untracked + ignored files (dangerous)

# Most destructive form: removes *everything* not tracked by Git
# Always run the dry-run first: git clean -ndx
git clean -fdx

Interactive mode (choose what to delete)

git clean -di
Safer alternative: If you’re not sure you want to delete untracked files, stash them instead: git stash -u (or git stash -a for ignored too). Then you can inspect and drop later. See Git Stash: Complete Guide.

2. What git clean deletes (and what it won’t)

git clean operates on files that are not tracked by Git. Think “junk that Git doesn’t know about”.

Type Example Will git clean delete it?
Tracked src/app.js (committed) No (use git restore / git reset / git rm)
Untracked tmp.txt (never added) Yes (with -f)
Ignored node_modules/, dist/, target/ Only with -X (ignored only) or -x (ignored + untracked)

This is the key mental model:

3. The safe workflow (status → dry-run → delete)

When in doubt, do this exact sequence:

# 1) See what’s going on
git status

# 2) Preview deletion
git clean -nd

# 3) Delete untracked (only after confirming)
git clean -fd
Do not skip step 2. If you delete the wrong local file (for example a .env you never committed), Git can’t bring it back.

4. Flags you actually need

You don’t need to memorize every option. These are the ones you’ll use constantly:

Flag Meaning When to use
-n Dry run (don’t delete) Always, before deleting
-f Force (required) Required to actually delete
-d Also remove directories When you want to remove folders like dist/
-X Remove only ignored files “Clean build output but keep other untracked”
-x Remove ignored files too “Nuke everything not tracked” (be careful)
-i Interactive mode When you want to choose file-by-file
-e <pattern> Exclude pattern Protect .env or other local files

5. Cleaning ignored files (node_modules, dist, target)

Most people reach for git clean because their repo contains huge ignored folders (like node_modules/) or build output (dist/, build/, target/).

Remove only ignored files and folders

# Dry run first
git clean -ndX

# Then remove ignored directories too
git clean -fdX

This is usually safer than -x because it won’t delete “random untracked stuff” you actually care about.

Remove ignored + untracked (full cleanup)

# Dry run first
git clean -ndx

# Full cleanup
git clean -fdx
Common footgun: -x can delete local config files you didn’t commit (for example a .env you forgot to add to .gitignore). If you want to protect something, use -e exclusions or stash first.

6. Interactive cleaning (git clean -di)

If you want an extra safety layer, use interactive mode. It walks you through what can be deleted and lets you choose:

git clean -di

Interactive mode is great when you have a mix of generated files and “real” local files, and you’re not confident your ignore patterns are perfect.

7. Excluding files and folders (-e)

Exclusions let you keep specific paths even when they’re untracked. This is especially useful for local secrets, environment files, or per-developer configs.

Example: keep a local .env file

# Preview deletion, but keep .env
git clean -nd -e .env

# Delete untracked files/dirs, but keep .env
git clean -fd -e .env

Example: clean everything except node_modules

# Maybe node_modules isn't ignored in this repo; keep it explicitly
git clean -fd -e node_modules/

-e uses Git’s ignore pattern rules (similar to .gitignore), so you can exclude folders, globs, and specific filenames.

8. Common real-world use cases

“My repo is messy after switching branches”

# Preview
git clean -nd

# Clean untracked files + directories
git clean -fd

“I want a clean slate before running a build”

# Remove ignored build output only (safer)
git clean -fdX

“CI should start from a pristine working tree”

# WARNING: destructive. Typical CI pattern.
git reset --hard
git clean -fdx

If you’re trying to discard edits to tracked files (not untracked junk), use git restore instead.

9. Cleaning submodules

Submodules have their own working trees. If the root repo is clean but a submodule is full of untracked files, clean them recursively:

# Preview in each submodule
git submodule foreach --recursive 'git clean -nd'

# Clean untracked files/dirs in each submodule
git submodule foreach --recursive 'git clean -fd'

For a deeper explanation of submodules (and how to avoid common pitfalls), see Git Submodules: Complete Guide.

10. Troubleshooting

“git clean does nothing”

Most often, one of these is true:

“It won’t remove directories”

Add -d:

git clean -fd

“I deleted the wrong file”

If the file was truly untracked, Git can’t recover it because it was never in history. For future safety:

“I actually wanted to discard edits to a tracked file”

That’s not what git clean is for. Use git restore path/to/file (see Git Restore).

FAQ

Why does git clean require -f?

Because deleting files is irreversible for untracked paths. Git forces you to add -f to avoid accidental deletion from a mistyped command.

How do I clean only one folder?

Pass a pathspec after -- to limit what’s cleaned:

# Only clean inside dist/
git clean -fd -- dist/

Should I use git clean or rm -rf?

rm -rf will delete anything you point it at. git clean is safer in a repo because it targets only untracked/ignored files and supports dry runs and interactive mode.


If you want a broader “what should I run?” decision guide for undo operations (including reset vs revert vs restore and recovery with reflog), see: Git Undo: Reset, Revert & Restore.