205

I can't delete a certain folder in Windows 7. It's not a system folder. It was created by an app. I get an error message "You need permission to perform this action. You require permission from Tony....". I am an admin and logged in as myself Tony. I have full ownership of the folder with full control permission. I rebooted the machine. I killed the app which created the folder. I am out of ideas.

Why is Windows 7 not letting me delete the folder?

studiohack
  • 13,468
  • 19
  • 88
  • 118
Tony_Henrich
  • 11,506
  • 28
  • 86
  • 116
  • 3
    Have you got any background processes: TortoiseSVN, Anti-virus softwares, etc. that might be hanging on the folder (though I agree this is unlikely after a reboot). Alternatively have you tried deleting it while in safe mode? – PeterJCLaw Oct 29 '10 at 18:33
  • No idea why, but if safe mode deletion doesn't work, you could boot your system from a live CD of some sort (like Linux) and go directly into the file system and delete the file. This would undoubtedly work, but if some process is actually using the file, then it might break that process (when you reboot into Windows) with unknown consequences for the rest of your system, so make a backup of the file so you can put it back if things start to go south. – Joe Feb 11 '13 at 19:47
  • Just a thought: What are the attributes of the folder and everything under it? If something is set to read only (or maybe system), then you would probably have to remove those attributes before you could delete it even if you own it and are an admin. I haven't worked at that level in Windows for a long time, but that's how it used to work. – Joe Feb 11 '13 at 19:54
  • You are not an administrator. You are said that you have his rights. Wrongly. The receipt is very simple, look this answer below: http://superuser.com/a/1119561/134259 – Gangnus Feb 06 '17 at 12:49
  • 3
    I am in the administrators group. – Tony_Henrich Feb 07 '17 at 16:14

15 Answers15

162

I am not sure why this happens but there is a workaround.

To take control of the folder that cannot be deleted, create a text file called delete.bat and add the following lines to it:

    SET DIRECTORY_NAME="C:\Locked Directory"
    TAKEOWN /f %DIRECTORY_NAME% /r /d y
    ICACLS %DIRECTORY_NAME% /grant administrators:F /t
    ICACLS %DIRECTORY_NAME% /reset /T
    PAUSE

You will need to change the directory path to match your requirements e.g. C:\Locked Directory to C:\Delete Me.

Right click on the file delete.bat select Run As Administrator and you should now have full control of the directory and all sub directories meaning you can do what you wish with them.

unrealapex
  • 132
  • 8
Sahil
  • 2,387
  • 2
  • 16
  • 10
  • 68
    Tried it but it didn't work. – Tony_Henrich Oct 29 '10 at 18:20
  • 2
    That is a bummer. I was faced with a similar issue on a Windows 7 (64 bit) machine and this worked like a charm. – Sahil Oct 29 '10 at 21:26
  • 7
    It would have been nice if Microsoft fixed this bug in Sp1. This is a massive fail, and I curse each time I have to delete deep nested directories. – ozczecho Apr 07 '11 at 06:12
  • 1
    +1 I had two system owned folders on a secondary drive that were created by the OS prior to my Win 8 -> 8.1 upgrade. Even through the Advanced Security dialog (MS's preferred approach), I could not take ownership to delete. This worked like a charm. – Joseph Ferris Sep 20 '13 at 14:49
  • 1
    Windows 8.1 - Files & Folders were created remotely through SMB, only this was able to take ownership. Afterwards I still could not remove the files (Error message told me to get permission from myself) but the Advanced Security Dialog was THEN able to fix the permissions using the inheritance clean-up checkbox. After this I could remove the files. – dualed Jun 11 '15 at 06:31
  • 1
    the second line just tells me "access is denied". – peter May 07 '18 at 06:58
  • Now it says I need permission from myself to delete the directory. I gave myself full control of the directory and logged out and back in. Same thing. – Josh Withee Aug 31 '18 at 14:14
  • 1
    Both commands succeed, I now have ownership for 'Administror' to directory. I then had to do 'rmdir /s %DIRECTORY_NAME%' in order to delete directory, even though it was empty, otherwise I got 'Access is denied.' – arberg Oct 20 '18 at 09:14
  • This worked for me (up to a point), but only after I changed "administrators" to my own login name. After that there were still a couple of files left that I could not delete because they were in use. Now to find out what program is using them. – Marnix A. van Ammers Nov 22 '18 at 21:46
  • ERROR: The current logged on user does not have ownership privileges on "...." --- aka, doesn't work – Yaakov5777 Apr 12 '19 at 06:51
  • 2
    Worked for me, this should be the accepted answer. – Maxim Egorushkin Apr 27 '19 at 22:56
  • 3
    This is the only thing that actually worked for me after trying too many things. Thanks a lot! – techfly Oct 04 '19 at 09:13
  • 1
    Note that both the /d y and administrator groups are language specific and you'll need to change those for the ones of the language of the OS – Winter Feb 02 '20 at 22:05
  • `D:\somepath\lib>TAKEOWN /f dlib /r /d y` Result: `ERROR: Access is denied.` – Tomáš Zato Jan 14 '21 at 17:21
  • whoa! I completely butchered the permissions for a folder on a network drive: The only assigned permissions was a `Deny` rule to delete the folder O.o I thought I was done for. I can't believe this worked (Ran `.bat` as a domain Administrator) – velkoon May 18 '21 at 14:49
  • Same on Windows 10 – FantomX1 Jun 26 '21 at 19:39
  • After running the script, i attempted to delete my bizarre directory but only part of it was deleted. To delete the rest, I rebooted into safe mode, ran the script again and then I was able to delete them successfully! – machine_1 Sep 05 '22 at 19:36
77

The only way was to delete files starting from the lowest level folder in that folder. I got that error message with every folder which had subfolders. I deleted all files/subfolders working my way up.

Tony_Henrich
  • 11,506
  • 28
  • 86
  • 116
  • 16
    That's the only thing that worked for me as well. – BioGeek Apr 05 '12 at 10:06
  • http://stackoverflow.com/questions/1293398/under-which-circumstances-does-the-system-process-pid-4-retain-an-open-file-ha. have a look here. worked for me – tigrou Sep 15 '12 at 12:57
  • 6
    Try this: Properties -> Security -> Advanced. Ensure the owner is assigned full control permissions. Change Permissions, check "Replace all child object permissions...", Apply. Now try to delete. The analog solved same problem for me under Win8, if certain details are slightly different. – wilee Nov 24 '12 at 17:37
  • You may also want to restart, because we're on Windows and that's a thing. Drove me crazy for 15 minutes, but a restart fixed it after following Wilee's instructions. – Vael Victus Apr 24 '15 at 23:21
  • I've built a small program [node-purgewindir](https://github.com/devotis/node-purgewindir) that does exactly that. Deletes a directory by deleting every subdirectory recursively starting with the deepest nested subdirectories first – Chris Apr 03 '16 at 08:42
  • 3
    why does this occur? – Jason S Jun 01 '16 at 18:42
  • 17
    I can't do this because "Access is denied" when trying to open it. – User Dec 08 '16 at 17:12
  • http://superuser.com/a/1119561/134259 – Gangnus Feb 06 '17 at 12:53
  • For me I just had to delete a single file, which was nested three levels deep, and then I could delete the rest. (Win 10) – Colm Bhandal Apr 19 '19 at 09:52
  • Wow. This works even after 11 years. – Vikas Apr 12 '21 at 04:40
  • If you can do this manually there must be a way to script it then? – pete Dec 02 '21 at 01:07
36

A folder can disallow a parent from overwriting its permissions, so it stops working at a certain depth.

Use Process Monitor and filter for ACCESS DENIED events to figure out this depth

Every time an ACCESS DENIED event occurs change the permissions as explained by @Sahil.


You can use Handle too see what processes are opening files within your folder.

handle C:\Path\To\Folder\You\Are\Trying\To\Delete

Example:

C:\Windows\system32>handle C:\Windows\System32\inetsrv

Handle v3.45
Copyright (C) 1997-2011 Mark Russinovich
Sysinternals - www.sysinternals.com

svchost.exe   pid: 1500  type: File  134: C:\Windows\System32\inetsrv\config\schema
svchost.exe   pid: 1500  type: File  1B4: C:\Windows\System32\inetsrv\config\schema
svchost.exe   pid: 1500  type: File  1BC: C:\Windows\System32\inetsrv\config
svchost.exe   pid: 1500  type: File  1CC: C:\Windows\System32\inetsrv\config
svchost.exe   pid: 1500  type: File  1D0: C:\Windows\System32\inetsrv\config
inetinfo.exe  pid: 1572  type: File  3C:  C:\Windows\System32\inetsrv\en-US\inetinfo.exe.mui
inetinfo.exe  pid: 1572  type: File  188: C:\Windows\System32\inetsrv\MBSchema.bin.00000000h
inetinfo.exe  pid: 1572  type: File  190: C:\Windows\System32\inetsrv\MBSchema.bin.00000000h
inetinfo.exe  pid: 1572  type: File  1BC: C:\Windows\System32\inetsrv\MetaBase.xml
inetinfo.exe  pid: 1572  type: File  1D4: C:\Windows\System32\inetsrv\MBSchema.xml
svchost.exe   pid: 1884  type: File  1AC: C:\Windows\System32\inetsrv\config\schema
svchost.exe   pid: 1884  type: File  1C0: C:\Windows\System32\inetsrv\config
svchost.exe   pid: 1884  type: File  1C4: C:\Windows\System32\inetsrv\config
Tamara Wijsman
  • 57,083
  • 27
  • 185
  • 256
  • 2
    This is a good tip. My problem was that I wanted to delete a folder, but I got an Access Denied error. The folder properties showed that I had no permissions to even change the ownership of the folder. I used Process Explorer and used the Find Handle search to find which process had a handle to the locked directory. Then I killed the process, and everything was working again. – Mas Nov 27 '12 at 15:03
  • 8
    Using `handle` command in cmd line is the only thing that worked for me! – Steven Xu Nov 18 '14 at 03:07
  • 1
    Thank you for your answer. `handle` utility helped a lot – Sharikov Vladislav May 11 '17 at 09:55
  • 1
    The `handle` program was wonderful. – MxNx Oct 05 '17 at 20:51
  • Worked great in Windows 10 too :) – Doug McLean Oct 31 '17 at 09:53
  • Thanks for the help, with your information I've managed to detect which process has a handle associated with that folder without using external programs. Using Resource monitor from windows 10 on `CPU` tab > `Associated handles` panel and there I type the folder name in the search bar. And found the associated process Id. – mihkov Mar 26 '19 at 09:35
  • Late to the party, but can confirm that this resolved my issue. – snomsnomsnom Nov 11 '19 at 13:35
29

Had this issue today with a Windows Update folder and would like to supplement the answer.

When permissions on a folder are correct (example below, with Admin being the administrator account I was logged in with):

enter image description here

But permissions on subfolders are different and do not propagate from the parent even though you tell them to, causing problems when trying to delete the parent folder:

enter image description here

Open a command prompt (personally, I did not need to elevate the prompt) and run

ICACLS <FolderName> /reset /T

enter image description here

To replace Access Control Lists with default ACLs, traversing all subfolders. After this try deleting the folder.

mxl_
  • 1,155
  • 9
  • 18
  • 4
    Still doesn't work for me: "Accès refusé." which mean "refused access". Thanks anyway, what a pain to get "root" access on windows pffff.:/ (I was in a command line started as administrator) – pdem Jan 09 '16 at 18:56
  • 2
    I'm getting `handle is invalid` error. Any idea what that means? – James Draper Feb 05 '17 at 18:31
  • Windows 10 Enterprise. Worked for me! – Milos Ljumovic Mar 07 '20 at 02:29
  • Yes, i tried everything from above answers but the reason was the permissions were not propagated to the downstream folders and files and so had to lot of manual work to get everything deleted. – Varun Sharma Mar 05 '22 at 03:32
  • Another alternative is to actually, enable inheritance as discussed in the above answers. – Varun Sharma Mar 05 '22 at 03:36
15

I had this issue and tried several of the answers here to no avail. However, I was able to simply delete the entire folder I was having problems with from an elevated command prompt like so:

rmdir "C:\path\to\folder" /s

Hope this helps someone.

Bryan
  • 758
  • 8
  • 23
  • This is exactly what I did too... can't believe I had to look through every answer to find someone else who did the same thing. IMHO should be the first thing anyone tries. – u8it Oct 20 '16 at 01:35
  • Yes, this works, but you have forgotten to mention, that you should open the cmd window as an administrator. It is NOT the same as being a user with rights of administrator. – Gangnus Feb 06 '17 at 12:46
  • 6
    Opened CMD as administator, but still: *C:\ws>rmdir "C:\ws\preact-material-components" /s C:\ws\preact-material-components, Are you sure (Y/N)? y Toegang geweigerd.* (**access denied**) – Stijn de Witt Apr 28 '18 at 14:11
  • 7
    Still gives me "Access is denied" – TrojanName Dec 12 '18 at 12:11
  • 2
    Combining this with another answer's suggestion of `icacls foldername /reset /T` worked for me. (I'd earlier also taken ownership of the folder) – M.M Jan 15 '20 at 01:52
  • This really works for me. Thank you! – NeilWang Jul 31 '21 at 03:02
  • Remove-Item : A positional parameter cannot be found that accepts argument '/s'. – banan3'14 Feb 10 '23 at 10:34
9

You can use Unlocker utility to delete such folders.

It is very simple to use and it is completely free.

ani627
  • 348
  • 3
  • 13
  • 1
    To be specific the software is IOBit Unlocker - the above link is an affiliate link. This suggestion worked perfectly for me on Windows 10 to delete a domain user locked download. – rjmoggach Jan 28 '19 at 19:44
  • @mogga: I'm pretty sure the link would have changed in the last 5 years now. – ani627 Jan 29 '19 at 13:44
  • Every other solution didnt work. It was locked completely. IOBit savec my ass. – Fredy31 Feb 27 '20 at 19:08
  • 1
    I tried most of solutions , like trying to use 'TAKEOWN' command , trying to change file permissions etc etc , however all I was getting was 'Access denied' error . Moreover when I tried listing group & owner of that folder (which was corrupted) through cygwin on windows , I was getting 'Unknown' user & group [drwxr-x--- 1 Unknown+User Unknown+Group 0 Sep 11 06:14 bin] . I tried using IOBit Unlocker & that worked like charm , it unlocked that folder & afterwards folder got deleted automatically . – sjethvani Sep 11 '20 at 13:43
  • 2
    This is the only thing that worked for me. – Bognar Dec 01 '20 at 16:27
6

download SysInternals Suite and use the following utilities

use Process Explorer and Process Monitor to find out whats happening with your file

or use the following stand alone utils

use AccessChk to get details not easily available via the GUI

use PSFILE to scan for any process that is holding the file open then try the MoveFile utility to schedule a file delete in the next boot cycle.

The answer you're looking for will show up in the output of one of these utilities

kloucks
  • 241
  • 2
  • 3
4

When I run into a folder/file I can't delete and rebooting doesn't free the file. I will add to the security setting of the file/folder the Everyone group and set the permissions to Deny Full Control. Then when I reboot the machine, what ever was using the file/folder starts, it won't be able to read/write or lock the file/folder. Finally with nothing being able to use the file/folder, you will be free to delete it.

Hope this helps.

Scott McClenning
  • 3,545
  • 18
  • 19
4

No go with Windows 7 Ultimate x64. Finally had to boot from a USB-based linux OS (which ignores file permissions in NTFS file systems) and delete the offending directory that way.

  • 1
    That's a bit much for a Windows user (even though I suggested the same thing before I read your answer ;) ), but an rm -rf of the subdirectory tree is sure a lot faster than the other methods! The issue is that something could break when Windows starts up again and the offending process or whatever can't find the folder/files. – Joe Feb 11 '13 at 20:05
2

I assume UAC is enabled and you are trying to delete the folder from Windows Explorer. You should run Windows Explorer as administrator (right click on the icon and select "Run as administrator") and then delete the folder.

The other alternative is to disable UAC, but I wouldn't recommend it.

ncardeli
  • 326
  • 1
  • 4
  • 1
    UAC is off and I am logged in as admin. I tried your method and still same problem. Plus UAC is just a warning and if you say Yes, it continues with its process. – Tony_Henrich Oct 29 '10 at 18:13
  • @Tony UAC does actually do some other things. I have some legacy applications that won't run under Windows 7 with UAC turned on even if you accept the prompts (in fact, even if you have the prompts turned so low you don't get any running these apps!) I really wish I knew WHY this is, but it is. – Shinrai Oct 29 '10 at 18:53
1

Well, this happens for files that were installed by some kind installer, like under the TrustedInstaller user.

You need to pwn the file first,

  1. Right click the file, go to Properties/Security/Advanced.
  2. Owner tab/Edit/Change the owner to you (Administrator), save.
  3. Now you can go back to Properties/Security/ and assume Full Control over the file
bobobobo
  • 5,302
  • 8
  • 46
  • 61
0

I had the same issue (Windows required permission from user X, while being connected as user X…):

enter image description here

I fixed it using unlocker (free, Microsoft Windows), then did:

enter image description here

enter image description here

After that, I could delete the directory without further issue.

Franck Dernoncourt
  • 20,384
  • 48
  • 186
  • 322
0

I had a folder that was completely hosed on Windows Server 2019. Couldn't take ownership, couldn't modify permissions, couldn't delete no matter what I tried.

Ended up being able to resolve it with the robocopy command using the /MIR switch with others.

robocopy \\source\data c:\data /R:10 /W:10 /MIR /SEC /EFSRAW /ZB /J /LOG:C:\LogFolder\logfile.log

I was syncing files and was able to clean up the same folder in the source location first, then /MIR deleted the folder in the source with the other switches above.

DISCLAIMER: /MIR will mirror source directory to destination, including deleting files in destination, so be sure you want destination to match source exactly before running it!

lightwing
  • 83
  • 2
  • 10
0

For me, git's bash.exe was holding my folder open, so I had to manually stop the bash tasks.

How to stop bash.exe tasks:

  1. Right-click the task bar
  2. Task Manager
  3. "Processes" tab
  4. Look for bash.exe
  5. For each bash.exe
    • Right-click > End Task
Travis Heeter
  • 669
  • 2
  • 8
  • 22
0

Try moving the folder to your Desktop and deleting it there, odd, but has worked for me under similar circumstances.

Tog
  • 4,975
  • 14
  • 37
  • 42