Delete old windows / program files from second drive?

20

11

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

Posted 2015-05-15T11:39:50.107

Reputation: 547

Answers

52

  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 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

Posted 2015-05-15T11:39:50.107

Reputation: 612

4For the Windows folder just rename it to Windows.old and with the disk cleanup tool/ program you can simply delete it – SuperDJ – 2018-05-25T10:50:53.027

2Cacls is now deprecated, please use Icacls. – Tharindu Sathischandra – 2019-02-02T15:49:35.070

1@Tharindu thanks, I updated the answer with icacls now. Please verify that it looks correct – Arne H. Bitubekk – 2019-02-22T13:47:13.000

1rmdir is now rd – RaisingAgent – 2019-11-10T01:14:18.200

@RaisingAgent thanks for headups, I updated the code snippit with rd instead of rmdir – Arne H. Bitubekk – 2019-11-11T08:19:12.943

20

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

Posted 2015-05-15T11:39:50.107

Reputation: 393

This worked for the "Windows" folder. Arne HD's 3 commands did work for some other folders such as "Program Files" and ProgramData. – gus – 2017-06-27T16:24:05.047

Smart! And Extremely simple. Worked like a charm. – oneavi – 2018-01-06T09:56:29.070

1It works with any folder if you change the name to Windows.old. Awesome. – AdnanG – 2019-11-14T00:44:42.103

This is the best method, I tried 3 of the methods described here and this one was the best. – RaRdEvA – 2019-11-24T18:20:29.993

3

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

Posted 2015-05-15T11:39:50.107

Reputation: 31

1

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

Posted 2015-05-15T11:39:50.107

Reputation: 11

1

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

Posted 2015-05-15T11:39:50.107

Reputation: 111

Tested, it works! thanks! – GoTo – 2019-09-05T22:32:50.857

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

Madhav Biju

Posted 2015-05-15T11:39:50.107

Reputation: 1

-2

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.

Steven Perszyk

Posted 2015-05-15T11:39:50.107

Reputation: 1

1I 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 – 2016-01-19T04:02:50.323

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 – 2016-01-19T23:51:35.553

1Easiest? Downloading 1GB and burning to USB? Is this really easier than issuing 3 commands? You seem to be a real Linux guru ;) – snayob – 2016-01-20T14:23:14.187

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 – 2016-01-20T14:24:35.790

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 – 2016-01-20T14:29:10.487

Command prompt is the main Linux interface for admins. – snayob – 2016-01-20T14:31:14.803

1I'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 – 2016-01-20T14:31:28.537

1Have no interest in discussing "EASIEST way to do it". Your statement is SHOUTING a completely false fact. – snayob – 2016-01-20T14:36:14.573

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 – 2016-01-20T15:42:28.220

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 – 2017-01-05T10:36:58.843

-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

Posted 2015-05-15T11:39:50.107

Reputation: 1

Uneducated guess. The NTFS file system uses user account SIDs in conjunction with access control lists, not the user account display names. – None – 2018-05-26T21:32:28.530

-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.

user221120

Posted 2015-05-15T11:39:50.107

Reputation: 1