How do I delete a file that is permission denied?

Trying to delete a file, only to get an error message that you don’t have permission can be incredibly frustrating. This comprehensive guide will walk you through the most common reasons for permission denied errors when trying to delete files, as well as the steps you need to take to successfully remove the file.

What Does “Permission Denied” Mean?

A “permission denied” error occurs when you attempt to access or modify a file or folder that you do not have rights to. This is a security measure to prevent unauthorized changes to important files and folders on a computer system.

On Windows, Linux, and macOS systems, every file and folder has an associated set of permissions that determine which users can access it and in what ways. There are three basic permission types:

  • Read – View or open the file
  • Write – Modify or delete the file
  • Execute – Run the file if it is a program or script

When you receive a “permission denied” error trying to delete a file, it means your user account does not have write permission for that file. As a result, you are not allowed to modify or delete it.

Common Causes of “Permission Denied”

There are a few common reasons you may encounter “permission denied” errors when attempting to delete files:

1. Incorrect File Permissions

The most straightforward cause is that the file permissions are explicitly set to prevent your user account from deleting the file. For example, only the root or admin user may have write permission.

2. Ownership Issues

Every file has an owner, which is generally the user account that created it. Owners always have full permissions to delete their own files. If you are trying to delete a file owned by another user, you will get “permission denied” unless that user has granted you access.

3. System Protected Files

Certain special files are protected by the operating system and can only be deleted by an admin user even if you own them. For example, Windows protects files in C:\Windows\System32.

4. Parent Folder Permissions

To delete a file, you need write permission not only for that file but also for the folder it is contained in. If the parent folder does not allow write access, you will also see “permission denied.”

5. Moving or Copying Files

If you move or copy a file to a new location where your user account does not have delete permission, you will no longer be able to delete it and get “permission denied.”

6. Changing User Accounts

If you created files while logged into one user account, then switch to a different user account that lacks delete permission on those files, you will not be able to remove them.

How to Delete Files With Permission Denied

When faced with a file you cannot delete due to permissions, you have a few options to resolve the issue:

Change File Permissions

The most direct fix is to modify the file permissions to allow your user account to delete the file. You can do this a few different ways:

  • Use your file manager – Most file managers like Windows Explorer or Finder allow you to right-click a file, access its properties, go to the permissions tab, and enable your user account for full control.
  • Use the chmod command – On Linux and macOS, use chmod to explicitly enable user write permission. For example: chmod u+w filename
  • Take ownership – On Linux and macOS, use chown to take ownership of the file, granting yourself full access to delete it. For example: chown username filename

Switch User Account

Log into an administrator, root, or other user account that has permissions to delete the file. Then you can remove the file while logged in as that privileged user.

Boot Into Safe Mode

On Windows, restart your computer and boot into Safe Mode. Safe Mode loads only the essential drivers and services, minimizing permissions issues. Log in with an admin account and delete the file.

Disable Permissions Temporarily

As an admin user, you can fully take ownership of the file and disable inheritance of permissions to delete it. Just be sure to re-enable permissions after deleting the file.

Reset Permissions

On Windows, you can use the tool SubInACL to reset permissions on registry keys and files, replacing them with defaults that should allow deletion.

Unhide Hidden Files

Sometimes write-protected system files are hidden. Unhide all hidden files and folders, then navigate to and delete the file if it is now visible.

Bypass Permissions

Use a tool like Unlocker for Windows or chflags on macOS to forcibly unlock file permissions long enough to delete protected files.

Delete From External Device

If deleting from an external drive like a USB key, try plugging the drive into a different computer where your user account has full admin rights, letting you delete the file.

Reinstall Operating System

As a last resort, backup your data, fully reinstall your operating system, and restore your files to essentially reset all permissions. Then you can delete the previously protected file.

Best Practices to Avoid Permission Issues

Following these tips will help prevent permission denied errors when deleting files in the future:

  • Always delete files while logged into an admin user account
  • Avoid saving important files to system directories like Program Files or Users\AppData
  • Manage permissions carefully when sharing files or folders with other users
  • Never delete protected system files unless absolutely necessary
  • Copy files instead of moving them to retain permissions
  • Make regular backups of critical files in case you need to reset permissions

How to Delete Files in Different Situations

The specific steps to delete permission denied files differs depending on your operating system and file location. Here are tips for common scenarios:

Deleting Files in Windows

To delete files with permission issues on Windows:

  1. Right click the file and select Properties > Security
  2. Click Edit, then Add to add your user account with full permission
  3. Click OK and re-attempt deleting the file
  4. If still denied, boot into Safe Mode and delete as admin user

Deleting Root Files on Linux

For root owned files on Linux:

  1. Open terminal and run sudo chown username /path/to/file
  2. Then run sudo chmod 777 /path/to/file to enable all permissions
  3. Delete the file as your normal user account

Deleting Files on macOS

On macOS:

  1. Select the file and press Command+I to Get Info
  2. Expand Sharing & Permissions and enable your user to Read & Write
  3. If denied, repeat steps logged in as an admin user

Deleting System Protected Files

To remove system protected files:

  1. Boot into Safe Mode (Windows) or Recovery Mode (macOS)
  2. Login as administrator user and navigate to file
  3. Take ownership and change permissions to delete the file

Deleting Files in Program Files

For files within Program Files or other protected directories:

  1. Unlock file permissions using Unlocker (Windows) or chflags (Mac)
  2. Delete the file within several minutes before protections re-enable
  3. Or, copy the file to your user folder, delete original, then delete copy

Conclusion

Dealing with denied permission errors when trying to delete a file can be incredibly frustrating. However, this usually occurs due to user account and file permission issues that can be resolved. By taking ownership of the file, modifying permissions, using administrative accounts, and employing specialized utilities, you can override protections and force delete almost any file.

Following best practices for file storage and sharing will help avoid these permission problems in the future. Understanding the most common causes for deletion failures along with the available solutions will allow you to swiftly take action to remove any file from your system when necessary.