83

I have this empty directory, but I keep getting the following error message:

Cannot remove test: Directory not empty

I know this question has been asked plenty of times but none of them helped.

I tried ls -la to make sure there were no hidden files, and there does not seem to be: enter image description here

I tried sudo rmdir test as well as sudo rm -rf test, and I just do not know what is wrong.

I read it might be a problem with the file system, but I have no idea how I would go about fixing that.

αғsнιη
  • 36,350
rbrick
  • 933

18 Answers18

76

I had the same problem on a external hard disk, I tried so many ways using command line, but I failed every time. That's what worked for me:

  1. Right click on folder
  2. Move to trash
  3. Empty trash

Yes, it's silly but it worked for me (I don't really know how and why, but the damned folder no longer exist)

David Foerster
  • 36,890
  • 56
  • 97
  • 151
13

I have win 10 + ubuntu dual system installed. And both systems share the windows parititions.

Recently, i also ran into unable to delete empty folders in those partitions under ubuntu. I can't find out solution to solve it under linux.

However, after i switch to windows, and run

chkdsk

via cmd for the target disk. Some errors checked out. and then i run

chkdsk /F

to fix disk error.

After it finish, i am able to delete those folders now.

e-cloud
  • 269
12

I had the same issue not able to remove directory as it is not empty.

This sequence of operations worked for me.

  1. From command line first

    sudo rmdir  --ignore-fail-on-non-empty folder-name-to-be-deleted
    

The above command helps ubuntu ignore directory is not empty.

  1. Then just go to the folder and Shift + Del. That is all.
Anwar
  • 77,855
Amit
  • 129
  • 1
  • 3
7

You could delete it by typing sudo rm -rf {dir_name}. The directory might have been set to read-only permission. I hope the given command can delete the folder.

Wolverine
  • 664
7

If the directory is part of a filesystem mounted with CIFS (aka samba), and it contains a file that is a broken symbolic link, then ls fails to mention that file. (I observe this bug on a CIFS client running 14.04.2 LTS, and a server running 12.04.5 LTS.)

So the directory is not empty, but (over CIFS) you have no way to see that. The file can only be seen, and thus can only be deleted, by a command running on the fileserver hosting that filesystem.

4

If you are using btrfs, it is possibly an empty directory with a non-zero i_size. You can check whether this is the case with:

stat -c %s test

The i_size of an empty folder in btrfs should be zero. In my case, I got 6160 with ~/.config/chromium/Default.

The suggested solution is to unmount the filesystem, run btrfs check to confirm the issue and check for other problematic directories, and finally run btrfs check --repair to fix. This operation is risky, though, so it's a good idea to backup files first.

Source: Btrfs Problem FAQ

Zanna
  • 72,312
wzhd
  • 166
4

GUI solution

  1. Move or cut & paste the folder to trash folder
  2. empty the trash

it is done.

Command-line solution

sudo mv folder_error/ .local/share/Trash

you can clear with trash-cli: trash-empty or

sudo rm -fr ~/.local/share/Trash/*
David Foerster
  • 36,890
  • 56
  • 97
  • 151
Kadir Y.
  • 426
2

try this command:

sudo lsof | grep deleted 

Check in the list if your directory is still in use. :D

If so, stop the service and you will be able delete the directory.

Eric Carvalho
  • 55,453
ashwin2011
  • 59
  • 1
  • 4
2

I had the same issue on Ubuntu 16.04 and I fixed it by:

  1. emptying the trash folder
  2. rebooting

Opening and closing the file manager did no good—only rebooting worked.

dale
  • 21
1

Check if directory is not active for any running application before deleting. I was unable to delete a directory with same message, I found out the reason was that the directory was open in an node.js application. After closing/exiting the application I was able to delete the folder.

1

In my case, I had a separate command that ran concurrently and added files while the delete was ongoing; likely rm finished deleting everything it had planned to delete, then tried to delete the folder at the end, saw it had contents, and then threw an error saying 'yo, something is not right'.

(It had to do with running code in serial vs. accidentally in parallel for me.)

You may consider this if you're dealing with this problem.

Kyle Baker
  • 425
  • 4
  • 13
1

I ran into this issue and found that the solution was quite simple for me.

I had a Node server running that periodically accessed the files in the folder I was trying to remove. This caused several small meta files to constantly exist inside the folder. The purpose of these files is beyond me.

Even rm -rf folder/* would not remove these files permanently, despite showing the message removed folder/file.xyz.

I suspected the server, shut it down and removed the files and the folder without fail.

1

This problem appears when those folders or files are not copied completely. It's Input/Output Error. I tried to delete with Shift+Del or through commands, but these did not work. I tried right click and "Move to Trash" and it worked.

techraf
  • 3,316
0

Maybe the directory or a file in it has an immutable bit set?

root@lina:~# mkdir test
root@lina:~# chattr +i test
root@lina:~# rmdir test
rmdir: konnte 'test' nicht entfernen: Die Operation ist nicht erlaubt
root@lina:~# lsattr -d test
----i---------e---- test
root@lina:~# chattr -i test
root@lina:~# rmdir test

then you should remove the immutable bit with ''chattr -i'' first.

Ralph
  • 1
0

From the rm manual

       -d, --dir
              remove empty directories

So, to extend your example, try removing the directory with rm -drf test/

0

If you have dual boot (Windows + Ubuntu) and if the Windows did not shut down properly or shut down all of a sudden (electricity outage) while some apps are used/files are read/written then you might face this error.

... File is read-only
Cannot remove /path/to/dir: Directory is not empty

There are multiple solution to this problem. You can choose any one at time.

  1. Shut down Ubuntu + Turn on computer and open Windows. Ensure all apps are closed and shut it down and then turn on computer and choose Ubuntu
  2. Follow below steps (by staying in Ubuntu only without switching back to Windows)

  1. Go to Disks desktop app in Ubuntu enter image description here

  2. Select/click your storage disk

  3. Select the partition you are facing errors with in Volumes section

  4. Click on settings icon (bottom left corner in Volumes section) enter image description here

  5. Click "Repair Filesystem"

  6. Mount the partition

The problem is solved.

Source

-1

you may use rm command recursive flag i.e.

rm -r folder/

Example

-2

I solved a similar problem:

The actual error I got is:

find: cannot delete ‘/home/lucid/x/SecurityCameras/B-Yard’: Directory not empty

The command I used was:

find /home/lucid/x/SecurityCameras/B-Yard -mtime +15 -delete

The directory inside /home/lucid/x/SecurityCameras/B-Yard is 2021, which is put there by the camera. Deleting OR renaming the directory to something else and then back to 2021 FIXED the problem. Renaming and back again for B-Yard didn't do anything. So just renaming the offending directory to something else, and then back to the original name fixes the problem. No need to delete anything.

Zanna
  • 72,312
Norman
  • 1