“How do you force delete a folder in Linux (Ubuntu)?” - Quora
This is a common issue for Linux users who have trouble deleting a directory for various reasons, such as permissions or files within the directory that are still in use. This guide will cover the different steps and commands used to rmdir force remove the directory in Linux. Whether you are a beginner or an experienced Linux user, this article will provide the information and resources you need to force delete a file in Linux successfully.
Part 1. Commands to Force Remove File or Directory in Linux
You can use the rmdir or rm command to remove a directory in Linux. The rmdir or rm -d commands are applicable for empty directories, whereas the rm -r command is helpful for non-empty directories.
Before removing a directory, you must identify its name using the ls command for listing files and directories and the pwd command for finding out the current directory location.
The options chosen with these commands play an important role in their function. Here's a brief summary of rm command options:
Command |
Safety Level |
Best For... |
Force Capability |
| rmdir | Safe | Empty directories only. | Low (Only removes hierarchy) |
| rm -r | Moderate | Directories with files (Prompts if protected). | Medium |
| rm -rf | Dangerous | Force removing everything without prompts. | High (The Ultimate Solution) |

Part 2. How To Force Remove an Empty Directory in Linux?
To force delete an empty directory in Linux, use the rmdir command. The command has the following syntax:
rmdir [options] [directory name]
You can also combine the rmdir with additional options for specific removing demand.
The rmdir command has the following options:
- –ignore-fail-on-non-empty: Does not display an error message when attempting to remove a non-empty directory.
- -p: Removes the directory and its parent from the hierarchy.
- -v: Produces a verbose output.
- –help: Shows help text.
- –version: Indicates the command version.
Video Tutorial: How I Better Manage My Data on Linux?
Part 3. How To Force Remove a Directory That Contains Files or Sub-directories
Step 1. To force remove a directory with files and subdirectories, run the rm command with the -rf option:
rm -rf dirName
Step 2. Assume you have a directory called /tmp/data/ that contains the following two files and one directory:
ls -l /tmp/data/
Step 3. If you run the rmdir command, you will receive the following error:
rmdir /tmp/data/
Step 4. As previously stated, rmdir only deletes empty directories. To remove an entire directory in Linux, you must use the rm command:
rm -rf /tmp/data/
Step 5. Check it out:
ls -l /tmp/data/

Pro Tip: How to "Preview" a Force Deletion
The rm -rf command is irreversible. To avoid accidentally deleting the wrong folder, you can use the -i flag (Interactive Mode) or perform a "preview" by listing the files first:
ls -R dirName
Once you are certain, proceed with rm -rf. For even greater safety, always use absolute paths (e.g., rm -rf /home/user/folder/) rather than relative paths to ensure you are in the correct directory.
Part 4. How To Force Remove a Full Directory in Linux With Verbose Output
Step 1. Assign the -v flag to the rm command as follows:
rm -rfv dirname
Step 2. For example, you will delete a full directory named /tmp/bar. Take note of the output on the screen:
rm -rfv /tmp/bar/
Wherein:
- -r: Recursive delete
- -f: Delete a directory by force
- -v: Produces verbose output

Part 5. Getting the Permission Denied Message? How To Fix
You must have appropriate directory access permissions to force remove a directory in linux. Otherwise, the rmdir screen will display a permission-denied message.
Step 1. Use the prefix sudo at the start of the rm command to avoid this error:
sudo rm -rf dirName
Or:
sudo rm -rf /somedir/
Step 2. To delete a folder whose name begins with a “-,” such as “-backups,” enter one of the following commands:
rm -rfv -- -backups/
Or:
rm -rfv ./-backups/
Advanced Fix: "Operation Not Permitted" even with Sudo?
If sudo rm -rf still won't delete the directory, the files may have the Immutable attribute set. Linux uses this to prevent even the root user from deleting critical files.
Check for the attribute: lsattr -d dirName
If you see an i, remove it: sudo chattr -i dirName
Now, run the force remove command again: sudo rm -rf dirName
Part 6. How To Recover a Forced Removed Directory in Linux?
Every PC user eventually deals with data loss. Whether a hard drive crash or an unintentional file deletion, we have all encountered situations where we wished we could recover the destroyed data. Luckily, Linux makes it simpler to restore deleted files than other operating systems. We have picked out eight workable ways to recover deleted files in Linux.
Summing Up
Linux command-line deletion of directories and files requires properly utilizing the rm and rmdir commands. The rm command removes files and non-empty directories, whereas the rmdir command only removes empty folders.
Remember that Linux does not have a recycle bin or garbage folder. Using the command line, deleting files and folders will result in their permanent deletion. Therefore, before deleting the files and directories on your Linux, make a secure data backup with Wondershare Recoverit.
1. What is the difference between rmdir and rm -rf?
rmdir is designed to remove only empty directories and will fail if any files are inside. rm -rf is a recursive force command that deletes the directory and every file/subdirectory inside it without asking for permission.
2. Can I undo rm -rf in Linux?
Unlike a GUI desktop, the Linux command line does not have a "Trash" bin. Once you run rm -rf, the pointers to that data are removed. To recover these files, you must immediately stop writing to the drive and use a tool like Wondershare Recoverit for Linux.
3. How do I force delete a folder with a space in the name?
If your folder is named My Photos, use quotes: rm -rf "My Photos". Without quotes, Linux will try to delete two separate folders named "My" and "Photos."