# git clean: delete untracked files, with a dry run first

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-clean-full-en
`git clean` deletes untracked files; `-n` previews, `-f` deletes, `-d` includes directories, `-x` includes ignored files.

> **Warning:** this page deletes files. Nothing here can be restored through Git. Run every command in a throwaway directory created with `mktemp -d`, and read the dry run before removing the `-n`.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-14-A, TE-14-B and TE-14-C. The expected non-zero exit is TE-14-C (exit status 128). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-14-A: git clean -f leaves ignored files; -x removes them; -n removes nothing

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > a.txt; git add a.txt; git commit -qm one
printf '*.log\n' > .gitignore; git add .gitignore; git commit -qm gi >/dev/null
printf 'x\n' > junk.tmp; printf 'y\n' > keep.log
```

Paths written as `../name` sit one level above the repository folder, inside the same throwaway tree, so they do not appear in `git status`.

Command:

```bash
git clean -n | tee ../dry.out; echo still_there=$(ls junk.tmp 2>/dev/null | wc -l); git clean -f >/dev/null; echo after_f_junk=$(ls junk.tmp 2>/dev/null | wc -l); echo after_f_log=$(ls keep.log 2>/dev/null | wc -l); echo preview_x=$(git clean -ndx | wc -l); git clean -fdx >/dev/null; echo after_x_log=$(ls keep.log 2>/dev/null | wc -l)
```

Recorded output:

```
Would remove junk.tmp
still_there=1
after_f_junk=0
after_f_log=1
preview_x=1
after_x_log=0
```

### TE-14-B: git clean -f leaves an untracked directory; -fd removes it

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > a.txt; git add a.txt; git commit -qm one
mkdir -p newdir; printf 'x\n' > newdir/f
```

Command:

```bash
git clean -f >/dev/null; echo dir_after_f=$([ -d newdir ] && echo yes || echo no); git clean -fd >/dev/null; echo dir_after_fd=$([ -d newdir ] && echo yes || echo no)
```

Recorded output:

```
dir_after_f=yes
dir_after_fd=no
```

### TE-14-C: expected non-zero exit: git clean without -f, -n or -i refuses to run and removes nothing

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
printf 'u\n' > u.txt
```

Command:

```bash
git clean; rc=$?; ls u.txt; exit $rc
```

Recorded output:

```
u.txt
```

Standard error from the same run:

```
fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean
```

Exit status: 128. This status and the output match what was written before the run.

TE-14-C: git clean with no option exited with status 128 and printed fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean. The ls after it printed u.txt, so the untracked file was still there.

## Limits

Data loss is permanent in Git terms; dry run first; confined to the test directory.

## Source

- https://git-scm.com/docs/git-clean

## Evidence

- `TE-14-A`: git clean -f leaves ignored files; -x removes them; -n removes nothing
- `TE-14-B`: git clean -f leaves an untracked directory; -fd removes it

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.


> **Warning:** this example is destructive. Run it only in a throwaway directory, not against real files.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-14-A` | git clean -f leaves ignored files; -x removes them; -n removes nothing | 0 | PASS | `3b4516d4e967` |
| `TE-14-B` | git clean -f leaves an untracked directory; -fd removes it | 0 | PASS | `73597514e016` |
| `TE-14-C` | expected non-zero exit: git clean without -f, -n or -i refuses to run and removes nothing | 128 | PASS | `7f648fbf6431` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [git tag: name a commit, lightweight and annotated](https://kairos-2z6s.onrender.com/res/git-tag-full-en)
- [git log: commits reachable from HEAD, and --all](https://kairos-2z6s.onrender.com/res/git-log-filters-full-en)
- [Git Commit Allow Empty: a commit with no changes](https://kairos-2z6s.onrender.com/res/git-commit-allow-empty-full-en)
- [Git Branch Rename: change a branch name](https://kairos-2z6s.onrender.com/res/git-branch-rename-full-en)
- [Git Branch Delete: -d versus -D](https://kairos-2z6s.onrender.com/res/git-branch-delete-full-en)
