67

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.

7
  • 1
    Try sudo fsck /
    – ike
    Dec 29, 2014 at 4:38
  • 5
    Is it a normal directory or a mount point. If this is a mount point, it should be unmounted before deleting. If it is a normal directory, sometime there might be some open handles cause this problem. Reboot your machine once and try to delete it.
    – vembutech
    Dec 29, 2014 at 5:12
  • @rbrick Does test is your login user's name? what is the output of echo $USER? and what echo $HOME? Dec 29, 2014 at 11:52
  • 2
    I am confused, why does the user not have execute permissions on the directory, but group does? Aug 3, 2015 at 4:45
  • 2
    possible duplicate of How to delete a non-empty directory in Terminal? Aug 27, 2015 at 14:57

16 Answers 16

64

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)

9
  • 1
    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, 2017 at 7:50
  • 4
    What just happened here? Why did it work? Feb 8, 2018 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. May 28, 2018 at 10:58
  • 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 7, 2018 at 13:38
  • 1
    This just moves the corrupted folders from .Trash-1000/files to .Trash-1000/expunged where they remain undeletable. Sep 24, 2018 at 22:07
10

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.

2
  • 3
    This method works for me. I think it is also important to note that the shared partition(s) is NTFS formatted and that it is probably corrupted.
    – krismath
    Jul 19, 2018 at 9:25
  • Luckily still have access access to a native Windows machine. For all others, ntfsfix (existing in most distros) might be of help. (feedback if that works appreciated!) Mar 13 at 8:24
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.

4
7

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.
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.

2
  • Gah, this was it! Is it me, or is CIFS just buggy and slow as f****?
    – kontextify
    Feb 20 at 9:02
  • Seven years later and still no fix means it's not just you! Feb 21 at 16:34
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

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/*
1
  • 2
    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. Jul 20, 2018 at 21:57
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.

1
  • Obviously the directory won't show up in the list of open, deleted file descriptors if it can't be deleted. ;-] Jul 18, 2017 at 13:18
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.

1

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.

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.

1
  • Same here - I had a concurrent process running an un-tar command into that directory Feb 23, 2021 at 0:31
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.

2
  • This answer seems really edge-casey … 🤔
    – matigo
    May 25, 2021 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, 2021 at 11:42
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.

0

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.