62

My PC has a bunch of extra drives. Most of them contain old copies of Windows and Program Files. My PC is called "PC", and my admin user is called "Tim". I've assigned ownership of these second drives to the user "PC\Tim", and also given the user "PC\Tim" full control on the drives. I then try to delete either the Windows or Program Files folders on these drives, and get the message.

"You require permission from PC\Tim to make changes to this folder."

The current owner of these folders is listed as "Tim (PC\Tim)". The effective permissions for these folders lists this user has every right.

The absurd "answers" on the microsoft community of course offer things like "Use Disk Cleanup" (which is a blatantly uneducated answer) or "just reformat the drive" (formatting avoids the problem rather than addressing it).

What are the correct steps to delete these old files?

frumbert
  • 997
  • 2
  • 10
  • 19

9 Answers9

110
  1. Open a Command Prompt with administrative privileges.

  2. Run following commands, one at a time (change "Z:\Program Files" with folder you want to delete):

     takeown /F "Z:\Program Files" /A /R /D Y
     icacls "Z:\Program Files" /T /grant administrators:F
     rd /s /q "Z:\Program Files"
    

Note 1 - OS Language: takeown ... /D Y The input Y stands for 'Yes' and will be different depending on OS Language. Program Files folder and administrators may also be named differently.

Note 2 - Older versions of Windows: If icacls and rd are not supported then try use cacls and rmdir instead

Explanation and documentation:

The issue might be that you do not have the correct permissions in the discretionary access control lists (DACLs) for the folder and its content. DACLs identifies the trustees that are allowed or denied access to a securable object. So simply giving the ownership to the folder might not be enough, but you also needs to grant permission in the DACLs. You can grant permission using the icacls command as shown as example above. Access Control Lists and DACLs explained

takeown takeown documentation Administrator recovers access to a directories and it's content that previously was denied, by making the administrators group the owner. /F [directory] specifies which directory, /A gives ownership to administrators group, /R performs it as recursive operation on directory, all files and sub-directories, /D suppresses confirmation prompts when user does not have "List Folder" permission with following Y option which take ownership of the directory. (Note: The Y option may be different depending on OS language).

icacls icacls documentation Grants the administrators group full access DAC permissions to directory. [directory] specify which directory, /T performs the operation on all specified files in directory and sub-directories, /grant grants specified user access rights with :F which gives full access. (Note: The group name administrators may be different depending on OS language)

rd rd documentation Deletes the directory with all its sub-directories and files. /s deletes the specified directory and its sub-directories including all files, /q specifies quiet mode so you get no prompt for confirmation, [directory] specify which directory to delete.

Arne H. Bitubekk
  • 1,198
  • 2
  • 8
  • 12
  • 16
    For the `Windows` folder just rename it to `Windows.old` and with the disk cleanup tool/ program you can simply delete it – SuperDJ May 25 '18 at 10:50
  • 4
    `Cacls` is now deprecated, please use `Icacls`. – Tharindu Sathischandra Feb 02 '19 at 15:49
  • 1
    @Tharindu thanks, I updated the answer with icacls now. Please verify that it looks correct – Arne H. Bitubekk Feb 22 '19 at 13:47
  • 2
    ```rmdir``` is now ```rd``` – Penguin9 Nov 10 '19 at 01:14
  • 1
    @RaisingAgent thanks for headups, I updated the code snippit with rd instead of rmdir – Arne H. Bitubekk Nov 11 '19 at 08:19
  • "Access is denied", despite copy-pasting the commands with "D:\Windows" as the substitute in an Administrator command prompt. Renaming a folder to "Windows.old" and running disk cleanup worked, even for "Program Files". The name of "Program Files" switched back immediately but attempting to rename it still led to an option to remove it via disk clean up. – Matt Kleinsmith Dec 09 '20 at 20:37
  • not working for me - access denied on running rd – dermoritz Jun 15 '21 at 16:53
  • For the record, that last Y is just 'Y' and not /Y. – Keith Tyler Aug 24 '21 at 06:56
  • 1
    For spanish users is: 'takeown /F "Z:\Program Files" /A /R /D S' And 'administrators:F' becomes 'administradores:F' – driconmax Sep 11 '21 at 17:31
  • 1
    I've used this so many times now to clear old drives that I made a [snippet](https://gitlab.com/-/snippets/2384424) - potentially useful for others, so posting it here – Paul Benn Aug 07 '22 at 15:34
  • Fails to remove `Windows\WinSxS` and thus fails to remove the `Windows` folder – Torque Sep 22 '22 at 08:33
  • 1
    Will not work on modern Windows installs, especially for folders such as servicing, system32, SysWOW64, and WinSxS. Easiest solution is to just rename the Windows folder on the second drive to something else, such as Windows.old. – John Suit Oct 12 '22 at 20:02
61

What worked for me was a simplified version of this suggestion:

  1. Rename the Windows folder to Windows.old
  2. Run Disk Cleanup from the Start menu
  3. Select the drive containing the Windows.old folder
  4. Previous "Windows Installation(s)" then click on OK
x29a
  • 833
  • 7
  • 10
  • 1
    This worked for the "Windows" folder. Arne HD's 3 commands did work for some other folders such as "Program Files" and ProgramData. – Doochz Jun 27 '17 at 16:24
  • Smart! And Extremely simple. Worked like a charm. – oneavi Jan 06 '18 at 09:56
  • 9
    It works with any folder if you change the name to Windows.old. Awesome. – AdnanG Nov 14 '19 at 00:44
  • This is the best method, I tried 3 of the methods described here and this one was the best. – RaRdEvA Nov 24 '19 at 18:20
  • This is brilliant! I can't believe this works! The cmdline stuff worked for the other folders, but this finally worked! – MDT Guy Dec 05 '20 at 15:42
  • 1
    This works in December, 2020 for "Program Files", too. The command-based solutions didn't work for me. The name of the folder changed back automatically but this still set it as a target for disk cleanup. It didn't work for "Users", however. – Matt Kleinsmith Dec 09 '20 at 20:39
  • You can rename "Program Files" to "Windows.old" and use Disk Cleanup to move them as "Old Windows Installation", same as a real Windows folder. – Seth Apr 26 '21 at 22:27
  • This method works for all folders ... simple and effective. thanks – Mohammad Aldefrawy May 30 '23 at 11:49
  • @AdnanG Nailed it perfectly with applying this technique to any folder by simply renaming it. Awesome indeed. – slightly drifting Jun 29 '23 at 18:23
6

NOTE: Now that I think about it, you should be able to start from step 4, you just need to hit the "change permissions" button to get access. I'd test, but I'm out of directories to delete.

On Windows 10 (probably works with Windows 7 and 8 as well):

  1. Right-click on folder->properties->security->advanced.
  2. Click on Change next to owner (at the top). Enter your user name in the box, and hit check names. (Should convert into a MACHINENAME\USERNAME style.)
  3. Hit OK, OK, OK to get out of the security options.

You should now own the directory.

  1. Right-click on folder->properties->security->advanced. (Again.)
  2. Find "Users" in the permissions list. Click it, select edit, and choose full control.
  3. At the bottom, check "Replace all child permission entries...".
  4. Hit OK, then choose Yes in the warning box. Wait for the computer to apply the new permissions.

You should now have the permissions needed to delete the files. Do so. (Tested on both Program Files folders and Windows.old.)

user3757614
  • 161
  • 1
  • 1
4

This worked in Windows 10 SPANISH:

set del_folder=F:\Program Files
takeown /f "%del_folder%" /r /d S
icacls "%del_folder%" /grant administradores:F /T
rmdir /s /q "%del_folder%"

In the takeown command, you have to use an "S" at the end for "Sí" in Spanish, instead of "Y" for "Yes" (English).

For the icacls command, you need to use "administradores" (Spanish) and not "administrators" (English).

Juanga Covas
  • 141
  • 3
2

Simple rename Program Files to Windows.old and run disk clean to clear system files. Same trick work on any folder what you cannot remove.

Flegy
  • 21
  • 4
0

Create a live USB of your Linux distribution of choice, boot into that, mount the drives, and clean them up that way. Literally the easiest way to do it.

  • 1
    I think the question is more about file permissions/ownership than just deletion of files. If there is a possibility to achieve task using Windows tools why should anybody download say 500MB and more and create a bootable USB? I am also not 100% sure that every Linux distribution "of choice" can handle correctly all versions of NTFS. NTFS is not a Linux file system. Would you use Windows to manipulate files on Linux ext4 file system? – snayob Jan 19 '16 at 04:02
  • 1
    The Linux kernel can handle over 50 file systems. Also, it is literally the EASIEST way to do it. Maybe not what this user was looking for, but it is easy. – Steven Perszyk Jan 19 '16 at 23:51
  • 1
    Easiest? Downloading 1GB and burning to USB? Is this really easier than issuing 3 commands? You seem to be a real Linux guru ;) – snayob Jan 20 '16 at 14:23
  • Not quite sure what you mean by Linux guru. Most distros are 500 mb not 1 g. And lighter ones are only 300 mb. The only Linux distribution ive seen close to 1 gig is Ubuntu Studio, AV Linux, and maybe Kali Linux. But yes, using a simple gui that doesn't hide anything is generally easier than fighting the windows command prompt. – Steven Perszyk Jan 20 '16 at 14:24
  • Windows can handle far less file systems but is used on 90% of PC's ;) All Linux distributions are on less than 2% of all PC's. Linux is neither BSD nor OS X. – snayob Jan 20 '16 at 14:29
  • Command prompt is the main Linux interface for admins. – snayob Jan 20 '16 at 14:31
  • 2
    I'm not quite sure how that random and completely false "fact" pertains to the discussion on hand but thank you for enlightening me. If you want to have a personal discussion on why one would choose a GNU/Linux system, I'd be happy to have that discussion somewhere else. Other than that this discussion is getting terribly off topic – Steven Perszyk Jan 20 '16 at 14:31
  • 1
    Have no interest in discussing "EASIEST way to do it". Your statement is SHOUTING a completely false fact. – snayob Jan 20 '16 at 14:36
  • You, my friend, are not the op. Unless you can come up with legitimint reasons as to why my method is wrong, please shut up. Down vote my answer if you dont like it. – Steven Perszyk Jan 20 '16 at 15:42
  • I have a dual boot windows10/LinuxMint computer. When I boot into Linux, I can mount old windows drive only in readonly mode, because it says window drive is in hibernate state. So I cannot remove anything on the old windows system drive. – rfq Jan 05 '17 at 10:36
0

(For windows folder) If you can,change the name to windows.old.Then open disk cleanup,select the drive and click ok.After that click 'clean up system files' and it will delete the folder

Now if you can't rename the folder,download unlocker,open it and select the folder then click rename and change the name to Windows.old and then run disk cleanup. :D

-2

It could be possible that you already have ownership of the same user, due to it being a previous disk that was hosting your system. Remove that ownership and add it again (the same one). For example, if user PETER has ownership, remove it, apply to all subfolders, then add PETER again, and apply. You should be then able to first rename the folder you wish to delete, and then delete it.

Cheers

Guylee
  • 1
  • Uneducated guess. The NTFS file system uses user account SIDs in conjunction with access control lists, not the user account display names. –  May 26 '18 at 21:32
-2

Assuming that the deletion has to be made on a second drive: What has just worked for me is:

  1. install Lockhunter (free application to unlock and rename locked files) run Lockhunter with Admin privileges;

  2. rename the folder to delete to "Windows.old" (you will still read the original name after the renaming but don't worry the renaming will be confirmed by Lockhunter in it main window);

  3. run "Disk Cleanup" with administrative privileges;

  4. select the folder to delete that will appear with the name "Windows.old" in Disk Cleanup and delete it.