Rmdir is a command-line utility in Linux and Windows that can be used to delete non-empty directories. It is a useful utility compared to the standard rm utility because rm can delete directories even if they are not empty. It is helpful in cases where you only want to delete non-empty directories, and it prevents you from accidentally deleting directories that contain files.
The ‘rmdir: directory not empty’ error occurs when you run the rmdir command on a directory that is not empty, meaning it contains files or other directories inside it. This is the expected behavior of the rmdir command, and it will give you an error when you run it on a non-empty directory in Linux or other operating systems that have the rmdir utility.
How can I fix ‘rmdir: directory not empty’ in Linux?
There are a few simple fixes you can try in order to get rid of this error. Apart from running the command in a non-empty directory, this error can also occur due to symlink issues, among other things.
We will let you know about the various fixes you can try to get rid of this error.
1. Use the “rm -rf” parameter
You can use the ‘rm’ command line utility in Linux to delete directories that are not empty if you are encountering this error. To verify whether the directory is not empty and the error is not caused by anything else, simply issue an ‘ls -a’ command on the directory you want to delete. If you see files in the directory, as the output of ‘ls -a’ would show you, then you can issue the ‘rm -rf’ command to delete the directory.
We want to delete the directory abcd.
We issued an ‘ls -a’ command to check whether the directory was not empty, and it showed us that there were some files present inside the directory.
We can issue rmdir command on the directory but it will give us an error since the directory is not empty.
The simplest solution here is to use rm -rf on the directory.
rm -rf abcd
The -r option means recursive i.e delete everything inside the directory recursively (other directories inside it will be deleted too.)
The -f means to force delete, i.e in this case, the force option implies that delete the directory even if it’s not empty.
If you want to delete a directory on which your user doesn’t have write permissions, then you can use:
sudo rm -rf
Using sudo with rm is only recommended when you know what you are doing and the directory you want to delete isn’t a system directory as the root user can delete system directories too.
2. Use a file manager to delete the directory:
In some scenarios, for unknown reasons users have reported that they couldn’t delete files using the rm utility even by using the -f and -r flag and were able to delete the files using a file manager. You can also try that.
In Ubuntu and many other Linux distributions, file managers are installed by default. You can use such file managers to delete directories or folders as well.
If you are using Ubuntu, you can use the Nautilus file Manager which is installed in it by default to delete directories. Other file managers can be used as well. Nautilus is just an example.
Fix rmdir: directory not empty in Windows:
This error can also occur in Windows when trying to delete a directory using the command prompt or PowerShell. In order to fix this error in Windows, you can try the following:
- Use rm -rf method as shown in the Linux fix above.
- Make sure the directory you want to delete isn’t accessed by any other program. If it’s, use the task manager to kill that program.
Then try and delete the directory afterwards in cmd or powershell using rmdir or rm -rf