Linux ENOTEMPTY (errno 39) — Directory Not Empty

ENOTEMPTY (errno 39) means you tried to remove a directory that is not empty. This error occurs when calling rmdir() on a directory that still contains files or subdirectories. It is distinct from EISDIR (errno 21) because ENOTEMPTY specifically refers to a non-empty directory, not just any directory.

Common Causes

  • Attempting to rmdir a directory that contains files
  • Hidden files (starting with .) are still present
  • The directory contains subdirectories
  • A file is locked or in use within the directory

How to Fix ENOTEMPTY

1. List Directory Contents

Check what files remain in the directory:

ls -la /path/to/directory/

2. Remove Hidden Files

Hidden files are not shown by default:

ls -la /path/to/directory/ | grep "^\."
rm -rf /path/to/directory/.[!.]* /path/to/directory/..?*

3. Remove Everything Recursively

Use rm -rf to remove the directory and all contents:

rm -rf /path/to/directory/

4. Check for Locked Files

Find files that may be in use:

lsof +D /path/to/directory/
fuser -v /path/to/directory/

Kill any processes holding files open and retry:

fuser -k /path/to/directory/
rm -rf /path/to/directory/

Verification

After removal, confirm the directory is gone:

ls -la /path/to/

Comments