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

This post enumerates the 5 methods to fix the “Rmdir: Directory Not Empty” Error in Linux.

“How to solve "The directory is not empty" error when running rmdir command in a batch script?” - stack overflow

The error "Rmdir: Directory Not Empty" is a common problem in Linux systems. It happens when a user attempts to remove a non-empty directory on Linux, which makes it impossible to delete the directory using the rmdir command. The sections below cover how to solve or work around this error on Linux systems.

rmdir directory not empty error

Method 1: Remove Non-Empty Directory in Linux Using rm Command

The simplest solution is to use rm instead of rmdir. The rm command's basic syntax is rm <options> <file>. If the directory is empty, the "-d" flag removes it. However, in the case of non-empty directories, the "-r" flag can be used to delete the specified directory and its contents recursively.

  • As an example, consider the following:

rm -r directory

  • To force delete a directory, use the -f flag as follows:

rm -rf directory

  • If your directory path or name contains spaces, surround it with quotes:

rm -rf “directory”

  • To resolve any permission issues, use sudo:

sudo rm -rf directory

  • Finally, avoid using sudo rm -rf /, which will delete the contents of the root directory, i.e., every mounted filesystem.

Method 2: Delete Directory Not Empty in Linux Using File Browser

When the graphical user interface fails, we resort to the command line. As strange as it may sound, things are exactly the opposite in this case. Many users reported deleting the folder through the File Browser worked for them. It's worth a shot before moving on to the other options.

Step 1. Navigate to the File Browser and find the directory you want to delete. Right-click the file and choose Delete.

delete the directory not empty

Step 2. Confirm that the file has been deleted. The file has now been removed from the server.

confirm the deleted file

Method 3: Fix Rmdir: Directory Not Empty Error in Linux by Checking the File System

File system errors are a common cause of file deletion and other problems. As a result, the FSCK (File System Consistency Check) utility can be beneficial. The actions to take are as described in the following:

Step 1. Note the file system you want to run fsck with this command.

df -h

Step 2. Consider /dev/sda4 as an example. To unmount it, use this command. You'll need to use a live CD to unmount it if it's a root filesystem.

sudo umount /dev/sda4

Step 3. Then, run the following command. Follow the on-screen instructions to verify the restoration actions.

sudo fsck /dev/sda4

Step 4. After the repair, enter this command to remount the device and see if the problem has been resolved.

sudo mount /dev/sda4

Method 4: Remove Directory Not Empty From File Server

There have been instances where the directory could not be deleted because it was part of a CIFS-mounted filesystem (Samba). It failed to mention a file because it was a broken symbolic link. You couldn't see this over CIFS, so you may believe the directory is empty. In such cases, you must delete the file from the fileserver directly.

Method 5: Fix Rmdir: Directory Not Empty Error in Linux by Closing the Node App

Users have reported instances where the directory was open in a node.js app or where a node server was periodically accessing the files in the directory, resulting in minor meta files constantly existing inside the directory. In such cases, closing the application or server currently accessing the directory would resolve the problem.

Step 1. A Node.js application can be closed in various ways. If the application runs in a console, you can close it by pressing CTRL + C. However, to programmatically terminate a Node.js application, use the process module's exit() method.

The process object is a built-in module that contains information about the currently running Node.js process. It also lets you manage the current Node.js process from within the application.


Step 2. The process. exit() method instructs Node.js to terminate the process immediately. Any pending callbacks, network requests still in progress, database requests in progress, or filesystem access will be abruptly terminated.

You can also input a process.exit() an integer value to send an exit code to the operating system:


Step 3. The exit code is set to 0, which indicates success by default. Exit code 5 denotes a "Fatal Error," indicating that Node.js encountered an unrecoverable error. Each exit code has a unique meaning that you can use in your application to inform the operating system why your application was terminated.

You could also use the process.exitCode property to set the exit code:

process.exitCode = 5

Step 4. If you like to end the process gracefully, you must send a SIGTERM signal to the process and then use the process event handler to listen for this event.

Here's an example of a Node.js HTTP server that waits for the SIGTERM event to close the server nicely:

const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('Hello world!')

const server = app.listen(3500, () => console.log(`Server started!`))

process.on('SIGTERM', () => {
  server.close(() => console.log(`Server closed!`))

To send a SIGTERM signal to the current Node.js process, use the process.kill() method:

process.kill(, "SIGTERM");

What To Do If You've Mistakenly Deleted a Non-Empty Directory in Linux?

Accidentally deleting a non-empty directory in Linux can be a frustrating and time-consuming experience. Fortunately, tools are available to help recover deleted data in Linux, including Wondershare Recoverit Linux Recovery.

Recoverit Linux Recovery is a powerful data recovery tool specifically designed for Linux systems. Its advanced algorithms and user-friendly interface make Recoverit easy to recover deleted files and directories from Linux. Some of its key features include:

Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software

5,481,435 people have downloaded it.

Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.

Compatible with all mainstream Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.

Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.

The simple point-and-click interface allows you to recover data from Linux hard drives in just a few clicks.

Works through a remote connection. You can recover lost data even when your Linux device is crashed.

Wondershare Recoverit simplifies Linux data recovery. In three simple steps, you can recover your data.

Step 1. Select "Linux Recovery"

Start the Wondershare Recoverit software on your Linux device. Then, choose the NAS and Linux options from the main interface's left side. Click Linux Recovery to continue.

click linux recovery

Step 2. Connect to Your Linux Computer

A new window will pop up on your screen, as seen below. To create a remote connection, enter the necessary data. Click the blue Connect button once you're done.

establish a remote connection in linux

Recoverit will launch an automatic scan to search for lost data on your Linux computer while you wait for the connection to be established.

Step 3. File Preview and Recovery

Depending on how much data is being scanned, the scanning procedure can take some time; the bottom bar shows the scanning status. The best feature of Recoverit is the ability to halt scanning whenever a file you want to restore is located.

automatic scan of deleted files

Recoverit allows you to examine the files when scanning is complete to ensure they are the ones you wish to recover. To save the deleted directory from your Linux machine, choose Recover now.

preview and recover deleted files

The software will ask you where you want to keep the recovered files on your device. Click Recover to get the restored data.

save recovered files to different location
Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

If reading the recovery instructions makes you uneasy, here's a video tutorial you can watch.

Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later


When the "rmdir" command is used to delete non-empty directories, the error "remove Directory not empty" occurs. This post explains why the error "remove Directory not empty" happens and how to resolve it. Various examples of removing non-empty directories are also shown for clarity. And if you've mistakenly deleted a non-empty directory in Linux, Recoverit Linux Recovery will come to rescue your day. Download it now!

You May Also Like

How To Remove Files or Directories Recursively in Linux

Here are the steps to recursively remove files or directories/folders in Linux

How To Use Foremost to Recover Files on Linux and Its Alternative

Here’s how to install and use Foremost to recover data in Linux and its best alternative.

How To Install and Use TestDisk on Linux and Its Alternative

Detailed here are the ways to install and use the TestDisk Linux data recovery tool, plus its alternative.

Top 10 Linux Partition Recovery Tools

Restore your deleted or lost data with the 10 best Linux partition recovery software.

How to Recover Deleted Files from Linux Partition?

Try these tested and proven methods to recover deleted files from Linux partitions.