How to fix Would remove directory/ in Git

GitBEGINNERLOWUpdated Dec 6, 2025· 153 views

This informational message appears during a git clean dry run, indicating that an untracked directory would be deleted. Use the -d flag with -f to actually remove directories, or review the output to ensure you won't lose important files.

Quick fix

Fixes Would remove directory/

git clean -n
  1. Understand what git clean will remove
  2. Remove untracked files only (without directories)
  3. Remove untracked files AND directories
  4. Use interactive mode for selective removal
git — error
git clean -n
Would remove directory/

What this error means

When you run `git clean -n` (dry run mode), Git shows you what it would delete without actually deleting anything. The "Would remove directory/" message indicates that an untracked directory exists in your working tree and would be removed if you ran the actual clean command. By default, `git clean` only removes untracked files, not directories. This is a safety mechanism to prevent accidentally deleting entire folder structures that may contain important work. To include directories in the clean operation, you must explicitly add the `-d` flag. The "Would remove" output is not an error—it's Git's way of giving you a preview before making potentially destructive changes. This allows you to review what will be deleted and abort if necessary. Since `git clean` permanently removes files that aren't tracked by Git, there's no way to recover them through Git (unlike tracked files which can be restored via `git checkout` or `git restore`).

Typical symptoms

  • Running git clean -n shows 'Would remove directory/' in the output
  • Git clean dry run lists directories but doesn't remove them when you run without -d
  • Untracked directories remain after running git clean -f without the -d flag
  • git status shows untracked directories that you want to remove
  • Build artifacts or temporary directories accumulate in your working tree

Common causes

  1. Running git clean in dry run mode (-n) which only shows what would happen
  2. Untracked directories exist in the working tree from builds, IDE files, or manual creation
  3. Missing the -d flag which is required to remove directories
  4. Build systems or scripts created temporary directories not listed in .gitignore
  5. Cloned submodules or nested git repositories that weren't properly initialized
  6. IDE or editor generated folders (node_modules, .idea, __pycache__) not in .gitignore

How to fix "Would remove directory/"

1Understand what git clean will remove

First, run a dry run to see exactly what would be deleted:

bash
git clean -n

This shows files only. To include directories in the preview:

bash
git clean -dn

Review the output carefully. The "Would remove" prefix indicates files and directories that will be permanently deleted.

2Remove untracked files only (without directories)

If you only want to remove untracked files but keep directories:

bash
git clean -f

The -f (force) flag is required because Git's default configuration (clean.requireForce) prevents accidental deletions.

3Remove untracked files AND directories

To remove both untracked files and directories, combine the -d and -f flags:

bash
git clean -df

This will delete:
- All untracked files
- All untracked directories (and their contents)

Warning: This action is irreversible. Files removed by git clean cannot be recovered through Git.

4Use interactive mode for selective removal

For more control over what gets deleted, use interactive mode:

bash
git clean -di

This presents a menu where you can:
- clean: Remove all listed items
- filter by pattern: Exclude files matching a pattern
- select by numbers: Choose specific items to delete
- ask each: Confirm each deletion individually
- quit: Exit without deleting anything
- help: Show available commands

5Remove ignored files as well

To also remove files that are ignored by .gitignore (like build artifacts):

bash
# Preview what will be removed (including ignored files)
git clean -dnx

# Actually remove (including ignored files)
git clean -dfx

To remove ONLY ignored files (keeping untracked non-ignored files):

bash
git clean -dfX

This is useful for cleaning build outputs while keeping your untracked configuration files.

6Exclude specific files or patterns

To exclude certain files from being cleaned:

bash
# Exclude a specific file
git clean -df -e "important-file.txt"

# Exclude files matching a pattern
git clean -df -e "*.log"

# Multiple exclusions
git clean -df -e "*.config" -e "local-settings/"

This is helpful when you want to clean most untracked files but preserve certain ones.

7Handle nested Git repositories

If git clean encounters untracked directories that contain .git subdirectories (nested repositories), it will refuse to remove them by default. To force removal:

bash
git clean -dff

The double -f is required to delete nested git repositories. Be very careful with this—you could lose an entire repository's history.

8Consider git stash as an alternative

If you're unsure about permanently deleting files, consider using git stash instead:

bash
# Stash untracked files (can be restored later)
git stash --include-untracked

# Or stash everything including ignored files
git stash --all

Unlike git clean, stashed changes can be retrieved with:

bash
git stash pop

Advanced notes

### Git Clean Flag Reference

| Flag | Description |
|------|-------------|
| -n, --dry-run | Preview only, don't delete anything |
| -f, --force | Required to actually delete files |
| -d | Include untracked directories |
| -x | Also remove ignored files |
| -X | Remove ONLY ignored files |
| -i, --interactive | Interactive mode for selective deletion |
| -e <pattern> | Exclude files matching pattern |
| -q, --quiet | Only report errors |

### Why Git Requires Force

The clean.requireForce configuration option (default: true) exists because git clean is destructive. Unlike most Git operations, files removed by git clean cannot be recovered—they're not tracked, so there's no commit to restore from.

To disable this protection (not recommended):

bash
git config --global clean.requireForce false

### Cleaning in CI/CD Pipelines

In build pipelines, a common pattern is:

bash
# Reset tracked files and clean everything
git reset --hard HEAD
git clean -dfx

This ensures a pristine working directory matching the repository state exactly. However, be aware this removes node_modules, build caches, and other files that may slow down builds.

### Working Directory Scope

git clean only operates on the current directory and its subdirectories. To clean the entire repository:

bash
cd $(git rev-parse --show-toplevel)
git clean -df

### Empty Directories

Git doesn't track empty directories. Sometimes git clean -dn shows directories that don't appear in git status. This is because git status focuses on files, while git clean with -d explicitly looks for untracked directories.

Frequently asked questions

What does "Would remove directory/" mean?
When you run git clean -n (dry run mode), Git shows you what it would delete without actually deleting anything. The "Would remove directory/" message indicates that an untracked directory exists in your working tree and would be removed if you ran the actual clean command. By default, git clean only removes untracked files, not directories.
How do I fix "Would remove directory/"?
This informational message appears during a git clean dry run, indicating that an untracked directory would be deleted. Use the -d flag with -f to actually remove directories, or review the output to ensure you won't lose important files.
What causes "Would remove directory/"?
Running git clean in dry run mode (-n) which only shows what would happen Untracked directories exist in the working tree from builds, IDE files, or manual creation Missing the -d flag which is required to remove directories
Is the "Would remove directory/" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading