Is it possible even as root one cannot remove directory? What could be the scenarios for directory not getting deleted?

Well nothing wrong with the file system.

There are applications running on the system perfectly.

Trying to delete directory with files and is not a mount/readonly.

Is that something to do with someone has reference to it or is in use not sure?

I am trying this from a shell script and cannot see the error message and need badly to get rid of this directory.

  • Do you get an error message when you try to delete a directory, and if so, what is that error message? – Pourquoi Litytestdata Apr 15 '09 at 21:13

Various possibilities:

  • File system mounted read-only
  • Immutable bit set (system specific)
  • From chaos pessimism: file system damage
  • From MarkR's good thinking: if it isn't empty, you can't delete it. Use ls -A to check for hidden files...

NFS normally excludes all write access for root (can be enabled with the no_root_squash mount option (but do not do this unless you absolutely trust absolutely all machines on your network; NFS is sometimes referred to as No File Security, and there is a reason for root being banned by default)).


Linux has regular permissions (read/write/execute) as well as extended attributes, which are less commonly known.

One of these is the immutable attribute which prevents even root from modifying the file.

The immutable bit is set with chattr +i file and unset with chattr -i file

Another one of these is the append attribute which only allows you to append to the file (not sure how this works with directories)

The append bit is set with chattr +a file and unset with chattr -i file


A non-empty directory cannot be removed with rmdir even if you're root. Also any other general problem such as readonly file system, io error etc.

  • Sure. Notice the obvious case. Hmph. If your going to be that way...+1 – dmckee Apr 15 '09 at 21:13

To rm a directory that contains something inside of it, you have to do a recursive remove. On my ArchLinux machine it would be $ rm -rf /path/to/dir.

Be careful, very careful.

$ rm -rf / your system and bye bye hd without any warnings.

  • That's why I alias rm with rm -I. – Bastien Léonard Apr 15 '09 at 21:22

Hum... OK, a few basic cases, some of them having been mentioned (hence the community wiki flag):

  • you are using rm without the -r arg

    rm -r directory
  • you are trying to delete from a readonly filesystem

    mount -o remount,rw /mount/point 
  • you are trying to delete from an NFS partition with root-squash

    (nothing you can do here, unless you are allowed to change the settings on the NFS server)

  • you are trying to delete from a read-only media (e.g. disk with the read-only pin set, mmc with lock on, etc...)

    Change the physical protection (may need a reboot)


Removing the directory . does not work:

$ sudo rm -rf .
rm: cannot remove directory `.'

Click here to see how to delete the current working directory

  • sorry ;-), fixed. Didn't paste the url but the links label as URL ;-) – Johannes Weiß Apr 15 '09 at 21:30
  • Happens all the time :) – avakar Apr 15 '09 at 21:32

Here are a couple things that come to mind:

  1. The directory or files within it are in use - you can use lsof (or something similar) to see if anyone has an open handle to something you're trying to delete
  2. The directory or files within it has special characters that need to be escaped to delete
  3. The directory or files within it has spaces that need to be escaped to delete
  4. The script is currently running in the directory you're trying to delete

File system damage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy