How do I delete a read-only file?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Deleting a read-only file usually means solving one of two problems: removing a file attribute, or overcoming a permission rule. Those are related but not identical, and the right fix depends on whether you are on Windows, Unix-like systems, or dealing with a file locked by another process.
Read-Only Does Not Always Mean the Same Thing
On Windows, "read-only" is often a file attribute. On Linux and macOS, the more important control is usually file or directory permissions.
That distinction matters because:
- removing a read-only attribute may be enough on Windows
- on Unix-like systems, deletion usually depends more on directory write permission than the file's own mode bits
- a file can also resist deletion because another process still holds it open
So the first step is to identify which of those mechanisms is actually blocking you.
Windows: Remove the Attribute First
In Command Prompt, attrib is the standard tool.
The -r flag removes the read-only attribute. After that, del can remove the file normally.
PowerShell offers a similar workflow.
If that still fails, the problem may be permissions or a file lock rather than the attribute itself.
Linux and macOS: Check Permissions and Ownership
On Unix-like systems, a file marked read-only is often just a file without write permission. But deletion usually depends on the parent directory being writable.
If removal fails due to permissions and you have the required privileges, you may need:
or, if ownership or directory permission is the real issue:
The crucial point is that rm removes the directory entry. If you can write to the containing directory, the file may be deletable even if the file itself is not writable.
File Locks Are a Different Problem
Sometimes a file looks "read-only" because deletion fails, but the real cause is that another process is using it.
On Windows, editors, antivirus tools, or background services can keep a handle open. On Unix-like systems, an open file can often still be unlinked, but the behavior depends on the exact scenario and what you are trying to accomplish.
If ordinary deletion fails unexpectedly, check whether the file is in use before changing permissions aggressively.
Deleting Programmatically
In Python, a common cross-platform pattern is:
This works well on Windows when the file's read-only attribute maps to a non-writable file mode. It is a practical way to make cleanup scripts more reliable.
In C#, you can do something similar.
Setting attributes to Normal clears flags such as read-only before deletion.
Be Careful With Recursive Deletion
If you are deleting a directory tree, read-only files inside it can cause partial failure.
The safe pattern is:
- traverse the tree
- clear read-only attributes where needed
- delete files
- then delete directories
Blind recursive deletion with elevated privileges is effective, but it is also a good way to remove the wrong thing quickly.
Common Pitfalls
The biggest mistake is assuming read-only always means a single file flag. Sometimes the real blocker is directory permission, ownership, or an open handle.
Another mistake is changing the file's write bit on Unix and expecting that alone to solve deletion, when the parent directory is actually the thing that controls removal.
A third issue is using elevated deletion commands without verifying the path carefully.
Finally, if a process still has the file open, attribute changes may not solve the problem until that process releases it.
Summary
- On Windows, remove the read-only attribute first, then delete the file.
- On Unix-like systems, deletion is often more about directory permissions than file mode bits.
- File locks and open handles can block deletion even when permissions look correct.
- Programmatic deletion often works by normalizing attributes before removal.
- Be especially careful with recursive or elevated deletion commands.
- Diagnose whether the problem is an attribute, a permission, or a lock before choosing the fix.

