Webinar: Role Based AI in One Click: Train, Deploy, and Use Across any Channel | December 17 at 11 AM EST.

Delete Folder in Linux

Updated on July 16, 2025, by Xcitium

Delete Folder in Linux

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 a 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 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Expand Your Knowledge

By clicking “Accept All" button, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Disclosure

Manage Consent Preferences

When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising.