How to Make File and Directory Undeletable Even By Root in Linux?
Last Updated :
01 May, 2025
In this article, we are going to see How to Make File and Directory Undeletable, Making a file or folder undeletable on Linux means setting it so no one—not even the root user (the all-powerful admin)—can delete, move, or change it. This is done using the chattr command, which tweaks special settings (called attributes) on files and folders. It is useful because it:
- Protects Important Stuff: Keeps critical files (like system configs or family photos) safe from mistakes.
- Boosts Security: Stops hackers or malware from deleting key files, even if they gain root access.
- Saves Time: No need to restore deleted files or worry about data loss.
- Great for Servers: Ensures Linux servers (e.g., on AWS or Azure) keep essential files intact.
How chattr Command Works
The chattr command (short for “change attributes”) is used to make something undeletable, you add the immutable attribute (marked with a +i), which blocks all changes, deletions, or moves. Only someone with superuser (admin) powers—using the sudo command—can set or remove this lock. You can check these settings with the lsattr command and remove the lock later if needed. We’ll guide you through this process step by step, making it as easy as locking a door.
So in order to make it Undeletable Syntax is
Note: We need superuser user privileges to set or remove the attribute sudo Command
sudo chattr +i - v "file name"
Example:
sudo chattr +i -V manager
The Above command, created an undeletable file that is inside an example directory.You can see the below image.

Suppose if you want to view the attributes of a file, use the lsattr command as shown below
lsattr manager
You can see in the below image the attributes that are set/

Now if we try to remove the immutable file which we have made some changes above using the command like in below
rm manager
sudo rm manager
We can see the below image after trying to remove it as both normal and root user

Next, if we want to make the Directory undeletable, then we should use -R Command to change the attributes and their files present inside.
sudo chattr +i -RV example1

To make a file mutable again we should use -i sign to remove all attributes as given below. Now you can see in the below image the example1 directory is now deletable because we have made it mutable using -i.

Conclusion
Even root users usually have powers—but sometimes, you need stronger protection. Using the simple chattr +i command, you can make critical files and directories completely undeletable—even from root-level mistakes or malicious attacks. In real-world server environments like AWS, Azure, and private Linux hosting, system failures often happen because someone accidentally deletes important configuration files. Setting key files as immutable prevents this before they happen.
Similar Reads
How to Fix - Unable to Lock the Administration Directory â Kali Linux Kali Linux is among the most famous OS for ethical hacking and pentesting. It contains all the necessary tools for pentesting and ethical hacking. Using Kali is not easy and every user has once encountered errors. In this article, we will see how to solve this particular problem using Kali Linux. Wh
5 min read
How to Manage Directories in Linux? Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
6 min read
How to Recover a Deleted File in Linux? We all have often faced a problem where we have accidentally deleted some files in Linux, that we regretted deleting later on after we did not even find it in the trash. But what if we can recover them? Here, we will discuss How we can recover a Deleted File in Linux.Whenever we delete something fro
4 min read
Ways to Permanently and Securely Delete âFiles and Directoriesâ in Linux Linux is an open-source operating system that is loved by tech geeks. It is a versatile operating system that can be used on thousands of different hardware configurations. The most interesting feature of Linux is its command line interface. This feature allows you to perform multiple operations wit
7 min read
How to Remove Directory in Linux In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files
5 min read