78

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нιη
  • 35,660
rbrick
  • 883

17 Answers17

70

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,264
  • 56
  • 94
  • 147
  • 2
    I got the same issue and your answer works. Not quite sure what the silly thing happened, and why "-rf" failed to remove an non-empty folder. – artm Jun 12 '17 at 07:50
  • 4
    What just happened here? Why did it work? – Sahil Arora Feb 08 '18 at 20:42
  • What happened? Honestly, i don't know. Maybe, the folder's file was corrupted for some reason: a flag like "is-empty" or the file counter like "how-many-file-inside" has been setted to an erroneous value. Sometimes, errors like this could be caused by damages on the hard disk. Why did it work? I don't know how commands are implemented (think about C), but I guess they check those flags and counters before acting (i mean, if file counter >0 then "error: directory not empty" and abort), while emptying the trash directly remove the folder and all its file and subfolder without any kind of check. – Marco Ottina May 28 '18 at 10:58
  • Maybe, deleting a folder through command line requires some conditions, like the emptiness of the folder, (because removing the folder's node from file system removes only that single linknode and requires the folder to be empty so no other file-node would be "forgot" through the file system due to losing the intermediate link to them, represented by the folder) .. and deleting a folder through right click -> delete or emptying the trash removes the folder AND all file and subfolders recursively, not caring of flags, counters and stuffs like that (so, no checks are performed) – Marco Ottina May 28 '18 at 11:06
  • I repeat, i don't know the exact reason, i'm just guessing, because i don't know how the file system, commands and the whole OS are implemented and programmed. – Marco Ottina May 28 '18 at 11:09
  • 1
    In my case, the subdirectory in question happened to be in a folder that was shared from my Mac to a linux VM, and I was trying to delete the folder from the linux VM side. It seems that the Mac side was holding onto some folders. Doing the rm -rf from the Mac side worked. – MindJuice Jun 07 '18 at 13:38
  • This moves the directory to .Trash where it sits prohibiting operations on the attributes of the entire drive. – Stephen Boston Aug 31 '18 at 20:39
  • 3
    This just moves the corrupted folders from .Trash-1000/files to .Trash-1000/expunged where they remain undeletable. – Sebastian Simon Sep 24 '18 at 22:07
  • Strange that sudo rm -rf did not work, but this did. @Sebastian, the corrupted folder did not reside in .Trash/expunged. – Rainer Glüge Jan 12 '21 at 09:56
11

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
  • 76,649
Amit
  • 111
11

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
  • 241
6

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
  • 654
6

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
  • 70,465
wzhd
  • 156
3

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,264
  • 56
  • 94
  • 147
Kadir Y.
  • 416
  • 3
    I think the command line solution here is actually a general solution. Move the empty folder to another location, and remove it once it's moved. E.g., something like this mkdir a ; mv test a/ ; rm -rf a/test ; rm -rf a. An approach like that worked for me (although I have no idea why it would), and I didn't need root access. – Jake Fisher Jul 20 '18 at 21:57
  • Your comment should be the accepted answer lol – Mattia Rasulo Nov 04 '22 at 21:20
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
  • 54,385
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
  • 415
  • 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.

  • This answer seems really edge-casey … – matigo May 25 '21 at 10:07
  • 1
    Very much an edge case. Nonetheless resulted in exactly this error, and my initial debugging endeavour lead me here. Perhaps it will help the next person who makes the same oversight I did ‍♂️ – rasmniel May 26 '21 at 11:42
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

you may use rm command recursive flag i.e.

rm -r folder/

Example

0

From the rm manual

       -d, --dir
              remove empty directories

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

-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
  • 70,465
Norman
  • 1