Migrate to RunxBuild and earn up to $50 in hosting credit on your first deposit.

Calculate your savings
unxBuild

Remove Directory Linux Not Empty: rm -rf, find -delete, and Safety

Sean

Platform Writer

Jul 05, 2026
4 min read

Remove directory Linux not empty: rm -rf works, find dir -delete is faster for many files, rmdir errors. The team that uses the right tool for the dataset size has efficient deletion. The team that previews the path before rm -rf has data preservation.

Remove Directory Linux Not Empty: rm -rf, find -delete, and Safety

Table of contents

rmdir vs rm -rf

rmdir /tmp/empty   # works for empty
rmdir /tmp/full    # error: Directory not empty
rm -rf /tmp/full   # works (careful!)

The team that uses rmdir for empty and rm -rf for non-empty has the right pattern.

rm -rf

rm -rf /path/to/dir

Standard way. The team that uses this has the tool everyone knows.

find -delete (faster for many files)

# Delete all files but keep dir
find /path/to/dir -mindepth 1 -delete

# Delete only files (keep subdirs)
find /path/to/dir -type f -delete

Faster than rm -rf for very large directories. The team that has many files uses find.

rsync —delete (efficient bulk)

rsync -a --delete --exclude='.git' /tmp/empty/ /path/to/dir/

Empties the target by syncing from empty. The team that uses this for fast cleanup.

Safety: confirm before delete

# Verify the path
ls -la /path/to/dir/

# Use absolute paths
rm -rf /var/log/old_app/  # not 'rm -rf ./old/' (relative path risk)

The team that previews has fewer accidents.

Recovery: trash-cli

# Install
sudo apt install trash-cli

# Use instead of rm
trash script.sh      # moves to ~/.local/share/Trash
trash-list           # see what's in trash
trash-restore        # restore from trash

The team that uses trash-cli has soft delete with recovery. Replaces rm with trash via alias.

FAQ

Why does rmdir fail with ‘Directory not empty’?

rmdir only removes empty directories. The team that uses rmdir has the safe-but-restrictive tool. The team that needs non-empty removal uses rm -rf.

How do I delete a directory with millions of files?

find -delete is faster. rsync —delete to empty target is also fast. The team that has huge caches uses these.

Can I undelete after rm -rf?

Only with backups. The team that has backups has the right answer.

What’s the safest rm command?

rm -i (interactive) or use trash-cli. The team that values safety has one of these in their workflow.

Can I delete a directory I don’t own?

Only with sudo. Without sudo, you get ‘Permission denied’.

If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.

Useful related references:

#linux#rm#rmdir#command#dev-infra