Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically solve rm cannot remove path : device or resource busy error

I am trying to remove a directory /path/to/dir using the rm -rf command. Unfortunately I get the error

rm: cannot remove '/path/to/dir/.nfsdda293a660f276ca0000000a': Device or resource busy

After a little bit of research, I realized that I need to find which process is using this file before I can delete it:

lsof /path/to/dir/.nfsdda293a660f276ca0000000a

which will return something with the PID associated with the process:

COMMAND   PID
python    28594

I then kill the PID and try again to delete, but I still get the initial error.

How to force the script to delete /path/to/dir automatically within a script, without manual intervention?

like image 302
JejeBelfort Avatar asked Feb 05 '26 14:02

JejeBelfort


1 Answers

This worked for me:

lsof +D /path

That will recurse through the filesystem under /path, so beware doing it on large directory trees.

Once you know which processes have files open, you can exit those apps, or kill them with the kill(1) command.

Credit: https://unix.stackexchange.com/questions/11238/how-to-get-over-device-or-resource-busy

like image 195
Hayden Avatar answered Feb 08 '26 04:02

Hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!