Updated on July 16, 2025, by Xcitium

Ever encountered the frustrating “Failed to remove directory not empty” message in Linux? You’re not alone. Whether you’re cleaning up directories, managing server space, or scripting automation, knowing how to delete folder in Linux is essential.

In this guide, we’ll explore everything from simple command-line removals to force deleting non-empty folders. Whether you’re a Linux beginner or a sysadmin, this article offers practical tips and examples to get the job done safely and efficiently.

Why Deleting Folders in Linux Can Be Tricky

Linux offers powerful command-line tools for directory management, but the catch is — it assumes you know exactly what you’re doing. One wrong flag and you might delete critical data or face permissions errors.

Common issues include:

  • Permissions denied

  • Directory not empty

  • Confusion between rmdir and rm -r

Let’s walk through every scenario and solve them step by step.

Basic Commands: How to Delete Folder in Linux Command Line

1. 🗑️ Using rmdir for Empty Directories

bash

CopyEdit

rmdir folder_name

 

This command only works if the folder is empty. If not, you’ll get the familiar error:
rmdir: failed to remove ‘folder_name’: Directory not empty

Best for: Removing unused, empty directories.

2. 🧹 Remove Non-Empty Directory in Linux with rm -r

bash

CopyEdit

rm -r folder_name

 

  • -r stands for recursive—removing the folder and all its contents.

  • Useful when folders contain subfolders and files.

⚠️ Be cautious — this will not prompt for confirmation.

3. 💣 Force Remove Directory in Linux Without Prompt

bash

CopyEdit

rm -rf folder_name

 

  • -f forces deletion, skipping permission prompts or warnings.

  • -r handles recursive deletion.

This is the go-to for developers and sysadmins who want to delete all files in a directory in Linux quickly.

Deleting Multiple Folders or Files at Once

To delete all subfolders within a directory:

bash

CopyEdit

rm -r folder1 folder2 folder3

 

To delete all files but keep the folder:

bash

CopyEdit

rm -f folder_name/*

 

To delete all files and subdirectories inside a folder:

bash

CopyEdit

rm -rf folder_name/*

 

What If You’re Getting a “Directory Not Empty” Error?

This error usually means:

  • You’re using rmdir on a non-empty directory

  • Hidden files (.filename) exist inside

  • Permission issues prevent removal

✅ Fixes:

  • Use ls -a to list hidden files

  • Run sudo rm -rf folder_name (requires sudo privileges)

Bonus: Automating Folder Deletion with Scripts

Want to clean up temp folders daily? Create a simple bash script:

bash

CopyEdit

#!/bin/bash

rm -rf /home/username/temp/*

echo “Temp cleaned!”

 

Schedule it with cron for automation.

Safety Tips Before Deleting Folders in Linux

To prevent accidental loss:

  • Always use ls folder_name before deletion

  • Run with -i flag for interactive confirmation:

bash

CopyEdit

rm -ri folder_name

 

This prompts for each file, preventing irreversible damage.

Summary Table of Key Commands

Task Command
Delete empty folder rmdir folder_name
Delete non-empty folder rm -r folder_name
Force delete folder rm -rf folder_name
Delete all files in folder rm -f folder_name/*
Delete all contents recursively rm -rf folder_name/*
Delete folder interactively rm -ri folder_name

Frequently Asked Questions (FAQs)

1. What is the difference between rm and rmdir?

  • rm is for removing files or folders.

  • rmdir only works on empty folders.

2. Can I recover a deleted folder in Linux?

No, not easily. Unless you have backups or recovery tools like TestDisk, deletion is typically permanent.

3. Why does it say “Failed to remove directory not empty”?

Because rmdir only removes empty folders. Use rm -r instead.

4. How do I delete a folder without confirmation?

Use rm -rf folder_name to remove folders without any prompts.

5. Is there a GUI way to delete folders in Linux?

Yes, in environments like Ubuntu, use File Manager to right-click and delete folders. Still, the terminal is more powerful and faster for bulk operations.

Final Thoughts: Clean Up Your Linux System Like a Pro

Whether you’re managing cloud servers, scripting daily tasks, or cleaning local directories, understanding how to delete folder in Linux is crucial. Use the right commands with care and always double-check before pressing Enter.

Want to learn more about system security and automated endpoint protection?

👉 Request a Free Demo from Xcitium and explore how our tools can keep your Linux and enterprise systems safe.

See our Unified Zero Trust (UZT) Platform in Action
Request Demo

Protect Against Zero-Day Threats
from Endpoints to Cloud Workloads

Product of the Year 2025
Newsletter Signup

Please give us a star rating based on your experience.

1 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 5 (1 votes, average: 5.00 out of 5, rated)Loading...
Expand Your Knowledge