28
Could not remove the folder [Folder name].

Show more details
 Error removing file: Directory not empty

Cancel-Skip All-Skip

I tried to delete these folders from terminal with:

rm -r
Lucio
  • 18,843
tepheno
  • 341
  • 2
    If that error output is from nautilus, you can add a screen-shoot to your question. Also, what is the output of rm -r from terminal? Have you tried with the answers of this post? – Lucio Mar 15 '13 at 15:50
  • 1
    use rm -rf instead – nickanor Mar 15 '13 at 16:21
  • I have been used rm -Rf for remove recursively directories, may be this work for you. The uppercase R means Recursive, see man pages man rm – revnoise Mar 15 '13 at 17:18
  • 1
    Do you have permission to remove this directory? Please execute ls -la and paste it here to discard this option. – revnoise Mar 15 '13 at 17:27
  • The folder was I think deleted first on windows and then tried to delete on Ubuntu. Then tried to delete on Windows but said error.

    Directory not empty

    but its all empty promise.

    On windows it says error Error x00000

    – tepheno Mar 16 '13 at 05:34
  • If rm -r won't work, rm -rf won't work either. From the man page: " -f, --force ignore nonexistent files and arguments, never prompt"

    If rm -rf does work, then rm -r would have simply given you a prompt to confirm you really wanted to delete the item.

    – zaTricky Sep 29 '15 at 15:54

8 Answers8

14

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.

snoop
  • 4,040
  • 9
  • 40
  • 58
wlraider70
  • 1,683
13

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.

jkd
  • 346
11

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

zaTricky
  • 437
  • 4
  • 10
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.

guntbert
  • 13,134
1

I have folders in Windows that are mounted to an Ubuntu VirtualBox virtual machine (VM). I could not remove them from the Windows file manager, or from inside the VM using rmdir or rm -Rf *.

TLDR: I was finally able to remove them in Git Bash (in Windows, of course), using rm -Rf *.

When I tried to delete in Windows file manager, it said I needed Administrator priveleges to delete the folders. But, even as Admin, it still failed.

From within the Ubuntu VM (where I have never had problems removing shared files before), I got the error cannot remove protocol error, as well as some others.

Finally, in Git Bash in Windows, I was able to delete the folders. I'm guessing that Git Bash had some sort of lock on the folders. In Git Bash, my working directory was one of the ones I was trying to delete. I backed out of it and then deleted it using rm -Rf without any error.

1

Go to the folder right before the one you want to delete and execute this command changing foldername by the name of your folder: rm -rf foldername

R means recursive, and f means force.

Ramon Suarez
  • 1,841
  • rm: cannot remove Wan Pisu - New World': Directory not empty – tepheno Mar 16 '13 at 05:33
  • Did you use -rf? – Ramon Suarez Mar 28 '13 at 11:50
  • -rf I think I used rm -r -f -v sir. – tepheno Mar 28 '13 at 13:59
  • I just did a test with the same directory name and it worked for me, also using your way of separating r f v. Are you sure you are using rm and not rmdir? Did the verbose (-v) give you any clues? Also, are there any 0 byte files within your directory? "rm -r -f -v Wan\ Pisu\ -\ New\ World/ removed 'Wan Pisu - New World/test.test' removed directory: 'Wan Pisu - New World'" – Ramon Suarez Mar 28 '13 at 16:53
  • no, they are only folders. Also when I'm deleting it on Windows it is Error; 0x----- cannot delete also. -v is explaining what is being done I know rm --help – tepheno Mar 29 '13 at 12:36
  • Picture here if this'll help link – tepheno Mar 29 '13 at 12:46
  • This article mentions hidden files as possible culprits: http://unix.stackexchange.com/questions/48227/cannot-remove-some-directory-directory-not-empty. – Ramon Suarez Mar 29 '13 at 17:51
  • vermins@ubuntu:/host/Vermin/Wan Pisu - New World$ find -exec ls -la {} \; total 40 drwxrwxrwx 1 root root 24576 Jan 6 00:33 . drwxrwxrwx 1 root root 16384 Mar 16 07:15 ..

    What should I do with this? AS A NEWBIE that was deep explanation. :(

    – tepheno Mar 29 '13 at 18:27
0

Try: rmdir --ignore-fail-on-non-empty *directory*

muru
  • 197,895
  • 55
  • 485
  • 740
Jamie
  • 111
0

If you don't have write permissions set, you cannot remove the directory.

In the below example even though I am an owner of the directory I cannot remove it. Once I add write permissions with chmod everything succeeds.

DIR:/xieerqi
skolodya@ubuntu:$ ls -dl TESTDIR/                                              
dr-xr-xr-x 2 xieerqi xieerqi 4096 Jan 24 16:06 TESTDIR//

DIR:/xieerqi skolodya@ubuntu:$ rm -rf TESTDIR/
rm: cannot remove ‘TESTDIR/one & two’: Permission denied rm: cannot remove ‘TESTDIR/tom&jerry’: Permission denied rm: cannot remove ‘TESTDIR/jekyl&hide’: Permission denied rm: cannot remove ‘TESTDIR/pdfReader.sh’: Permission denied

DIR:/xieerqi skolodya@ubuntu:$ chmod +w TESTDIR/

DIR:/xieerqi skolodya@ubuntu:$ rm -rf TESTDIR/

DIR:/xieerqi skolodya@ubuntu:$ echo $? 0

karel
  • 114,770
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • The only way to remove a directory with an zero size file in it that I could do on Ubuntu server (VPS), was as root with rm -rf directory. None of the above options worked for me. – Agguro Jun 27 '21 at 14:19