Why I can't delete a folder?

Solution 1:

Did you try sudo rm -r. What kind of files are there? You could also delete all the files first then delete the folder. It's extra work, but sometimes it is worth it to get the job done.

sudo is for superuser actions.

Solution 2:

I had this issue with a folder on a btrfs filesystem. Checking mount points and lsof are the normal things to look for. In my case it was corruption caused by an old bug, since fixed:

The normal things to check for:

  • lsof +D <path> - This will list all open files. You cannot delete a folder that contains open files, though that would also normally give a slightly different error: "Device or resource busy". Note that if you leave out the "+D <path>", lsof will show a listing for all folders on all filesystems - which will waste time. ;)
  • mount: This will show you the currently mounted filesystems. You cannot delete an actively-mounted mountpoint or its parent folders.
  • btrfs sub list <path>: Similar to a mount point, you cannot delete a btrfs subvolume except by using the btrfs-specific command: btrfs subvolume delete <path>.

Corruption Workaround

In the case of corruption where it cannot easily be fixed, the basic workaround is to back up, format the filesystem, and then restore (leaving out the bad data). Depending on how much data you have in the filesystem, this can be a chore. :-/

In my case the problem folder was inside a subvolume so I was able to do the above but by deleting only the subvolume instead of the entire filesystem. I backed up my data and deleted the subvolume: btrfs sub del <subvolume-path>. I was then able to restore into a new subvolume, leaving out the problem folder.

Corruption of i_size

The cause in my case was found to be corruption of i_size, where it was non-zero. Btrfs uses the i_size to tell whether or not the folder is empty. To check on this, run:

stat -c %s <foldername>

The i_size of an empty folder in btrfs should be zero. In ext it is typically 4k.

Related links:

https://btrfs.wiki.kernel.org/index.php/Problem_FAQ#I_cannot_delete_an_empty_directory

Solution 3:

Try cd into the directory, then remove all files using rm -rf *. Then try going out of the directory and use rmdir to delete the directory.

Solution 4:

Try sudo rm -rf [Folder name]

If it still displays "Directory not empty" that means that the directory is being used. Try to close it or check which program is using it then re-use the command.

Worked for me when I was trying to delete the Unity project and it was used by unity hub. simply closed unity hub and rewrote the command worked fine.