---
title: "Delete directories in Linux safely: rmdir, rm -r and find · CloudSat"
description: "Remove empty and full directories in Linux with rmdir, rm -r and find, add safety rails against rm -rf accidents, and fix permission denied and busy errors."
url: https://cloudsat.io/learn/delete-directories-in-linux-safely
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Linux](https://cloudsat.io/learn/topic/linux)

# Delete directories in Linux safely: rmdir, rm -r and find

Remove empty and full directories in Linux with rmdir, rm -r and find, add safety rails against rm -rf accidents, and fix permission denied and busy errors.

CloudSat team November 18, 2026 6 min read

On a Linux server there is no recycle bin. `rm` unlinks files immediately, and on ext4 or XFS you should treat that as final. Deleting a directory is easy; deleting exactly the directory you meant, and nothing else, is the part that deserves a method. This guide covers the right command for each case, the rails that stop a typo from wiping a system, and the errors you will meet along the way.

## Look before you delete

Spend ten seconds checking what is inside and how big it is:

```bash
ls -la /srv/app/releases/old
du -sh /srv/app/releases/old
find /srv/app/releases/old -type f | wc -l
findmnt -R /srv/app/releases/old
```

The last command matters more than it looks. If another filesystem is mounted somewhere below the directory, such as a data volume or a network share, a recursive delete walks into it and removes its contents too. `findmnt -R` prints nothing when no mount is involved.

## Pick the command

| Goal | Command |
| --- | --- |
| Remove an empty directory | `rmdir dir` or `rm -d dir` |
| Remove a chain of empty directories | `rmdir -p a/b/c` |
| Remove a directory and everything in it | `rm -r dir` |
| The same, with one confirmation prompt | `rm -rI dir` |
| The same, with no prompts at all | `rm -rf dir` |
| Empty a directory but keep it | `find dir -mindepth 1 -delete` |
| Delete only what matches a name, age or type | `find dir ... -delete` |

## Empty directories: rmdir

`rmdir` only removes empty directories, and that limitation is the point: it cannot destroy data by accident. If it reports `Directory not empty` on a folder that looks empty, there are hidden files:

```bash
ls -A cache/
rmdir -p build/tmp/cache
```

`rmdir -p` removes `cache`, then `tmp`, then `build`, stopping at the first one that still holds something. `rm -d` does the same job as plain `rmdir` and fits better in scripts that already use `rm`.

## Full directories: rm -r

`rm -r` removes a directory tree. The flags change how much it asks:

- `-i` prompts for every single file, which nobody answers honestly past the tenth one.
- `-I` prompts once before a recursive delete, which is the useful middle ground.
- `-f` never prompts and ignores files that do not exist.
- `-v` prints each removal, handy when you want a record.

```bash
rm -rI /srv/app/releases/old
```

Without `-f`, rm stops to ask about write-protected files (`remove write-protected regular file?`). That question is often a sign you are deleting something another user or service owns, so read it before you answer.

## Keep the folder, remove what is inside

`rm -rf /var/cache/app/*` looks right but has two flaws: the glob skips hidden files, and in a directory with hundreds of thousands of entries it fails with `Argument list too long`. Let `find` do it, previewing first:

```bash
find /var/cache/app -mindepth 1 -print | head
find /var/cache/app -mindepth 1 -delete
```

`-mindepth 1` protects the starting directory itself. `-delete` processes children before parents, so directories are empty by the time it reaches them.

## Targeted deletes with find

`find` decides what goes by name, age, type or depth. Always run the command with `-print` first, read the list, then swap in `-delete`.

```bash
find /var/log/app -type f -name '*.log' -mtime +30 -print
find /var/log/app -type f -name '*.log' -mtime +30 -delete
find /srv/uploads -type d -empty -delete
find /srv/data -mindepth 1 -maxdepth 1 ! -name keep -exec rm -rf {} +
```

The third line clears out empty folders left behind by an app. The fourth removes everything at the top level of `/srv/data` except the entry called `keep`. Avoid combining `-prune` with `-delete`: `-delete` implies depth-first processing, which makes `-prune` ineffective. Add `-xdev` to keep `find` on one filesystem, and never pipe `ls` into `xargs rm`, since names with spaces break it. If you need `xargs`, use `find ... -print0 | xargs -0 rm -f`.

## Safety rails

A few habits remove most of the risk:

- **Rely on `--preserve-root`.** GNU `rm` refuses to work recursively on `/` by default. Never add `--no-preserve-root`.
- **Add `--one-file-system`** to recursive deletes on servers with extra volumes. It skips any directory on a different filesystem from the one you named.
- **Guard variables in scripts.** If `TARGET_DIR` is empty, `rm -rf "$TARGET_DIR/build"` becomes `rm -rf /build`. The `:?` expansion aborts the script instead:

```bash
set -euo pipefail
rm -rf -- "${TARGET_DIR:?}/build"
```

- **Use `--` or `./` for odd names.** A directory called `-old` looks like an option; `rm -r -- -old` or `rm -r ./-old` removes it.
- **Chain `cd` with `&&`.** In `cd /srv/tmp; rm -rf ./*`, a failed `cd` runs the delete in whatever directory you were already in. `cd /srv/tmp && rm -rf ./*` stops at the failure.
- **Take a snapshot before a big cleanup.** On CloudSat, a snapshot from the console gives you a rollback point in case the wrong tree goes ([take a snapshot](https://cloudsat.io/docs/take-a-snapshot)), and a real [backup and restore plan](https://cloudsat.io/learn/vps-backup-and-restore-plan) covers the rest.

## Errors and what they mean

| Error | Cause | Fix |
| --- | --- | --- |
| `Directory not empty` | Hidden files remain | `ls -A`, then `rm -r` if you meant it |
| `Permission denied` | No write permission on the parent directory | Fix ownership, or use `sudo` if the files are really yours to remove |
| `Operation not permitted` | Immutable or append-only attribute | `lsattr -d path`, then `sudo chattr -i path` |
| `Device or resource busy` | The path is a mount point | `findmnt path`, then unmount it first |
| `Read-only file system` | The filesystem is mounted read-only | Find out why (often disk errors) before remounting |
| `Argument list too long` | A glob expanded to too many names | Use `find ... -delete` |

Permission to delete a file comes from the directory that contains it, not from the file itself. That is why you can sometimes delete a file you cannot read, and why a file you own can be impossible to delete from someone else's directory.

To find what keeps a path busy, `fuser -vm /mountpoint` lists the processes using a mounted filesystem, and `lsof +D /path` names every process with a file open under a directory. `lsof +D` reads the whole tree, so it can be slow on large directories.

## Trash instead of delete

On a desktop, sending files to the trash gives you an undo. GNOME systems include `gio trash`, and the `trash-cli` package adds `trash-put`, `trash-list`, `trash-restore` and `trash-empty` for any desktop:

```bash
gio trash old-project/
trash-put old-project/
trash-list
```

On a server, a trash folder only moves the space problem somewhere else. Snapshots and backups are the server-grade undo.

## FAQ

### Can I recover a directory deleted with rm -r?

Rarely, and not reliably. Stop writing to that filesystem at once, because new data overwrites the freed blocks. Recovery tools work on unmounted disks with mixed results. A backup or a snapshot is the only dependable way back.

### Why is disk space not freed after I deleted a large file?

A running process still has it open, so the data stays allocated until the process closes it. `sudo lsof +L1` lists deleted files that are still open. Restart the service that holds them, and the space comes back.

### Is there a difference between rm -r and rm -R?

No. Both mean recursive. `rmdir` and `rm -d`, on the other hand, only ever remove empty directories.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Linux](https://cloudsat.io/learn/topic/linux)

- [Back up and restore a VPS: a plan you can actually test](https://cloudsat.io/learn/vps-backup-and-restore-plan): Cloud and hosting Back up and restore a VPS: a plan you can actually test 6 min read
- [Keep Linux processes running after logout: nohup and beyond](https://cloudsat.io/learn/keep-linux-processes-running-after-logout): Linux Keep Linux processes running after logout: nohup and beyond 6 min read
- [Fix apt and systemctl command not found errors on Linux](https://cloudsat.io/learn/fix-apt-and-systemctl-command-not-found): Linux Fix apt and systemctl command not found errors on Linux 6 min read
- [Bash functions with arguments, return values and scope](https://cloudsat.io/learn/bash-functions-arguments-return-scope): Linux Bash functions with arguments, return values and scope 8 min read
