keepcalculator

06 Numbers and Powers

Rm Cannot Remove Device or Resource Busy? Fix

Card No. 06·66525 · Updated · Standard formula, shown below

If rm reports “Device or resource busy,” it means the file or directory is in use by a running process or mounted as a filesystem, and the kernel refuses to remove it while that reference exists. The fix is to identify and stop the process (or unmount the filesystem), then retry the removal. Below is a complete guide to understanding and resolving this error on Linux and macOS.

What “Device or resource busy” Actually Means

When you run rm on a file, the system calls unlink() to remove the directory entry. The kernel checks whether any process has the file open (or the directory is a mount point). If so, it returns EBUSY, which rm prints as “Device or resource busy.” This is a safety mechanism: deleting a file that is actively being written could corrupt data or crash the process.

The error is not a permissions issue (that would be “Permission denied”) and not a filesystem error (that would be “Input/output error”). It is specifically a reference count problem—the file’s inode is still linked to an open file descriptor or a mount point.

Common Causes

  • Open files: A process has the file open for reading or writing. This includes log files, configuration files, or data files used by daemons.
  • Current working directory: A process is using the directory as its working directory (e.g., a shell or a daemon started there).
  • Mount point: The directory is a mount point for a filesystem (e.g., a USB drive, a network share, or a bind mount). You cannot rm the mount point directory itself; you must unmount it first.
  • Kernel modules or swap: A file might be part of a kernel module or used as swap space, but these are rare on typical systems.

How to Identify the Process Holding the File

Before you can free the file, you need to know which process is using it. Use one of these tools:

  • lsof /path/to/file – Lists processes with open file descriptors on that file.
  • fuser -v /path/to/file – Shows the PID and user of processes using the file (Linux only).
  • lsof +D /path/to/directory – Lists all open files inside a directory tree (useful for directories).

Example output from lsof:

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3  1234 user    4w   REG  8,1        0   567 /var/log/app.log

Here, PID 1234 (python3) has the file open for writing. You can then decide to stop that process gracefully.

How to Free the File and Remove It

Once you know the PID, you have several options:

  1. Stop the process gracefully: Use kill 1234 (or kill -TERM 1234). Wait a few seconds, then try rm again.
  2. Force kill if necessary: If the process does not terminate, use kill -9 1234. This is a last resort because it does not allow cleanup.
  3. For a directory that is a mount point: Run umount /path/to/directory first, then rmdir the now-empty directory.
  4. If the process is a shell with the directory as CWD: Use lsof +D /path | grep cwd to find the shell, then cd out of it or kill it.

After freeing the file, rm should work. If it still fails, re-check with lsof—another process may have opened it in the meantime.

What If You Cannot Identify the Process?

Sometimes lsof shows no output, yet rm still says busy. Possible reasons:

  • Kernel threads or filesystems: Some files are used by the kernel itself (e.g., files under /proc or /sys). You should not remove those.
  • NFS or network mounts: The file may be locked by a client on another machine. Check with mount and consider using lsof on the server.
  • Deleted but open files: A process may have unlinked the file but still has it open. In that case, the file is already gone from the directory, but the space is not freed until the process closes it. You cannot see it in ls, but lsof will show it with a “deleted” marker.

If you are sure the file is not in use, a reboot will clear all references, but that is rarely necessary.

Preventing the Error in the Future

To avoid hitting this error:

  • Use fuser or lsof before deleting files in shared directories.
  • For log files, use logrotate to handle rotation and deletion safely.
  • If you are a developer, close file handles in your code before deleting temporary files.
  • For mount points, always unmount before removing the directory from your scripts.

Also, consider using rm -f to suppress error messages, but that does not bypass the busy check—it only hides the error.

Frequently Asked Questions

Can I force delete a busy file with rm -f?

No. The -f flag only ignores nonexistent files and suppresses prompts. The kernel still returns EBUSY, and the removal fails. You must free the file first.

Why does rm say busy for a directory I just created?

If the directory is empty and not a mount point, the most likely cause is that a process has it as its current working directory. Check with lsof +D /path and look for cwd entries.

What does “Text file busy” mean?

This is a related error (ETXTBSY) that occurs when you try to overwrite an executable file that is currently running. You can remove it, but you cannot write to it while it is executing. Use rm and then create a new file.

How do I remove a file that is part of a mounted filesystem?

You cannot remove the mount point directory itself. Unmount the filesystem with umount /path, then use rmdir to remove the now-empty directory. If the filesystem is busy, use lsof to find processes using it and stop them first.

Is it safe to use kill -9 on a process holding a file?

It is generally safe in the sense that it will release the file, but it can leave the process in an inconsistent state and may cause data loss if the process was writing. Prefer a graceful termination first.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →