Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Linux»How to Fix “Rmdir: Directory Not Empty” Error in Linux

How to Fix “Rmdir: Directory Not Empty” Error in Linux

AnupBy AnupAugust 16, 2022
rmdir directory not empty

The rmdir command is used to remove empty directories. If you attempt to remove a non-empty directory with this command, you’ll encounter the Rmdir Directory Not Empty error.

That much is intended behavior, but some users have reported cases where they faced this error despite using rmdir on an empty directory. This happens due to reasons like file system errors or broken symbolic links.

We’ve detailed various ways to solve or work around this error on both Linux and Windows systems in the sections below.

Table of Contents

  • Fix Rmdir Directory Not Empty Error on Linux
    • Use rm Command
    • Delete via GUI
    • Check File System
    • Delete from Fileserver
    • Node Fixes
  • Fix Rmdir Directory Not Empty on Windows
    • Resolve Common Issues
    • Work Around the Error

Fix Rmdir Directory Not Empty Error on Linux

The easiest fix is to use the rm command instead of rmdir. If this workaround doesn’t appeal to you, or the rm command doesn’t work either, the rest of the solutions will be helpful.

Use rm Command

The base syntax for the rm command is rm <options> <file>. The -d flag removes the directory if it’s empty. But in the case of non-empty directories, you can use the -r flag to recursively delete the specified directory and its contents.

  • For instance, here’s a basic example:
    rm -r directory
    rm-r-remove-directory
  • To force delete a directory, add the -f flag as such:
    rm -rf directory
  • If your directory path or name contains spaces, use quotes “” as such:
    rm -rf “directory”
  • Add sudo to resolve any permission issues as such:
    sudo rm -rf directory
    sudo-rm-rf

Finally, make sure you don’t use sudo rm -rf / as this would cause the contents of the root directory, i.e., every mounted filesystem, to be deleted.

Delete via GUI

When things don’t work in the GUI, we turn to the command line. As unusual as this may sound, things are the opposite in this case. Numerous users have reported that deleting the folder via the File Browser worked for them. It’s worth trying this before checking out the other solutions.

move-to-trash

Check File System

File system errors are a common reason for file deletion and similar issues. As such, using the fsck (File System Consistency Check) utility can be helpful. Here are the necessary steps for this:

  1. Use the df -h command and note the file system on which you want to run fsck.
  2. Let’s take /dev/sda4 as an example. Use the sudo umount /dev/sda4 command to unmount it. If it’s a root filesystem, you’ll need to boot from a live CD to be able to unmount it.
  3. Next, use sudo fsck /dev/sda4 and accept the prompts to approve the repair actions.
    sudo-fsck
  4. After the repair is complete, use the sudo mount /dev/sda4 command to remount the device and check if the issue is resolved now.

Delete from Fileserver

There have been cases where the directory couldn’t be deleted because it was part of a filesystem mounted with CIFS (Samba). As there was a file that was a broken symbolic link, ls failed to mention it.

Over CIFS, you wouldn’t be able to see this and thus could be mistakenly assuming that the directory is empty. In such cases, you would need to delete the file directly from the fileserver.

Node Fixes

Users have reported cases where the directory was open in a node.js app or where a node server was periodically accessing the files in the directory, causing minor meta files to exist inside the directory constantly.

In such cases, shutting down the application or server that’s accessing the directory at the moment would resolve the issue.

Fix Rmdir Directory Not Empty on Windows

While the application slightly differs, the idea behind fixing this error on Windows is very similar to the methods listed for Linux.

Resolve Common Issues

First, make sure no process is currently accessing the directory. Also, make sure you’re not trying to delete the directory you’re currently on, as that would also prompt this error. In such cases, change to a different directory before trying this.

Using the CHKDSK utility to resolve any file system issues has fixed this error for numerous errors. You can do the same with the chkdsk <drive letter> /f /r /x command.

chkdsk-f-r-x

Work Around the Error

Rmdir’s /s flag is meant to let you delete the specified directory and all the subdirectories and files. You can try a couple of things in cases where this doesn’t work as intended.

You could delete the contents of the directory before deleting the directory itself as such:

del /f /s /q directory 1>nul
rmdir /s /q directory

Alternatively, you could also try repeating the operation as shown below. Assuming there aren’t any permission issues, you should be able to delete the directory:

rmdir /s /q c:\directory
if exist c:\directory rmdir /s /q c:\directory
fix
Anup

Anup has been tinkering with PCs for over 15 years and writing professionally for almost 5. At Tech News Today, he mainly covers Windows systems, Linux, networking, and hardware-related topics.

Related Posts

how to change boot order on linux

How to Change Boot Order on Linux

December 16, 2022
make-command-not-found

How to Fix “Make: Command Not Found” Error

September 20, 2022
linux terminal for windows

How to Install Linux Terminal on Windows

September 19, 2022
How-to-force-Quit-an-app-on-Linux

How to Force Quit an App on Linux

August 30, 2022
best linux for virtualbox

8 Best Linux Distros to Try in VirtualBox

August 30, 2022
ifconfig command not found

How To Fix “ifconfig Command Not Found” Error?

December 18, 2022
View 2 Comments

2 Comments

  1. Austin on October 14, 2022 3:24 am

    I have a Perl script that I am running on a Linux system. The script does some simple analysis, and then gathers all of the data up, moving it into a final output folder. The script has a cleanup section at the end, which is meant to remove all the running/temp directories that were created throughout the analysis. The lines I have in that cleanup section that are causing errors are as follows:
    `rm -rf $CaseName` if (!$Debug );
    chdir( ” ..” );
    `rm -rf $TempFolder` if (!$Debug );

    The folder structure, from the root directory where the script is executed from, is:
    $TempFolder/$CaseName
    (the script creates the above folders during the analysis. The analysis is actually done within the $CaseName folder)

    So essentially, I am trying to clean up/remove the contents of $CaseName (which lives within $TempFolder), step up one directory, and remove $TempFolder.

    When I execute the script and it runs as it’s supposed to, I get errors during this cleanup portion saying that it cannot rm the folder because it is not empty (despite me having the -rf flag in there, which should force it to remove the folder anyway). But when I put a die statement right before that cleanup portion, and then execute each of the above commands manually on the command line, everything works out fine and the folders are removed as intended.

    Do you have any suggestions or ideas on what my issue might be here?

    Reply
    • Anup on October 14, 2022 11:22 am

      Hi Austin,
      The cleanup section seems fine. First, I recommend checking the things mentioned in the article if you haven’t yet done so (sudo, fsck, NAS shares, etc.). You could also run lsof /path/to/dir or lsof | grep "directory name" to check if any processes are running from said directory.

      Reply

Leave A Reply Cancel Reply

Latest Posts
what is ghost of tsushima legends

What is the Ghost of Tsushima Legends Mode

January 12, 2023
raid 5 vs raid 10

RAID 5 Vs RAID 10 – Which One Is Better?

January 12, 2023
best-tv-settings-for-gaming

Best TV Settings for Gaming

January 9, 2023
You may also like
tv-screen-goes-black-randomly

TV Screen Goes Black Randomly? Try These 11 Fixes

January 15, 2023
how to find unique values in excel

How to Find Unique Values in Excel

January 15, 2023
how-to-open-and-use-chrome-settings

How to Open and Use Chrome Settings

January 16, 2023
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Editorial Guidelines
  • Fact-Checking Policy
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday.

Type above and press Enter to search. Press Esc to cancel.