How to remove untracked files in Git (git clean) - Rocketeers

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com) 

 On this page

 Knowledge
---------

How to remove untracked files in Git (git clean)
================================================

### [\#Development](https://rocketee.rs/index.php/development)

git clean deletes untracked files from your working directory. Always dry-run it first with -n, because unlike most Git operations these deletions cannot be undone.

 Published by [Mark van Eijk](https://rocketee.rs/index.php/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [Preview first with a dry run](#content-preview-first-with-a-dry-run)
2. [Remove untracked files](#content-remove-untracked-files)
3. [Including ignored files](#content-including-ignored-files)
4. [Interactive mode](#content-interactive-mode)
5. [clean vs reset](#content-clean-vs-reset)

[\#](#content-preview-first-with-a-dry-run "Permalink")Preview first with a dry run
-----------------------------------------------------------------------------------

`git clean` permanently deletes files that Git is not tracking. Untracked files were never committed, so there is no history to recover them from. **Always preview** with `-n` (dry run) before deleting anything:

 ```
git clean -n

```

This lists what *would* be removed without touching anything.

[\#](#content-remove-untracked-files "Permalink")Remove untracked files
-----------------------------------------------------------------------

When you are happy with the preview, `-f` (force) actually deletes them. Git requires the flag as a safety measure:

 ```
git clean -f

```

To also remove untracked **directories**, add `-d`:

 ```
git clean -fd

```

[\#](#content-including-ignored-files "Permalink")Including ignored files
-------------------------------------------------------------------------

By default `git clean` leaves files listed in `.gitignore` alone (your `node_modules`, build output, `.env`, and so on). To remove ignored files as well, add `-x`:

 ```
git clean -fdx

```

To remove **only** ignored files and keep other untracked ones, use `-X` (uppercase):

 ```
git clean -fdX

```

[\#](#content-interactive-mode "Permalink")Interactive mode
-----------------------------------------------------------

If you want to choose file by file, use interactive mode:

 ```
git clean -i

```

[\#](#content-clean-vs-reset "Permalink")clean vs reset
-------------------------------------------------------

These two solve different problems:

- `git clean` removes **untracked** files (files Git has never recorded).
- [git reset --hard](/git-reset-hard) discards changes to **tracked** files.

To stop tracking a file that was committed by mistake but keep it on disk, that is a different task again, see [Removing tracked files in Git that should have been ignored](/removing-tracked-files-in-git-that-should-have-been-ignored).

### Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#Development](https://rocketee.rs/index.php/development)

- [Install PHP memcached extension on macOS](https://rocketee.rs/index.php/install-php-memcached-extension-on-macos)
- [How to delete a local (and remote) Git branch](https://rocketee.rs/index.php/git-delete-local-branch)
- [Fix: Cannot connect to the Docker daemon at unix:///var/run/docker.sock](https://rocketee.rs/index.php/cannot-connect-to-docker-daemon)
- [git stash pop vs apply: save and restore changes](https://rocketee.rs/index.php/git-stash-pop)
- [How to clean up Docker with prune (images, volumes, system)](https://rocketee.rs/index.php/docker-prune)
- [What port does SSH use (and how to change it)](https://rocketee.rs/index.php/ssh-port)

 [View all 12 articles →](https://rocketee.rs/index.php/development)
