The question of how to delete stubborn files in Windows is one that comes up often. This short howto has been developed in the hope that a simple, coherent procedure will make the process as painless as possible as well a provide a consistent plan of attack. That said, it would be wise to make sure that the object file is truly one you wish to remove since the removal of a critical system file can/will produce undesirable results as well as additional work for the remover.*
Start with the simplest, although if you're reading this for more than the exercise of casual reading, you've likely already tried and the simplest didn't work. Still, the beginning is a good place to start. Pick the file in Explorer and delete it.
If that doesn't work, go to command line and try to delete it. If you get an "access denied" error, attrib the file using the commandThis command removes the readonly, system and hidden attributes from the file and, hopefully, should make the file easier to remove. Attempt to delete the file again.Code:attrib -r -s -h <filename>
Sometimes Explorer decides to protect a file, even when deleting from the command line. If this happens, bring up the Task Manager, move to the Processes tab, find the Explorer process and terminate it. If you don't already have a command window open, click on File, pick New Task, type cmd and hit enter. You should be able to delete the file without any further issue.
If you are using Windows XP Pro or Windows 2000, you have another option to try - taking ownership of the file.
1. If using Windows XP Pro, open an folder view windows, click on Tools->Folder Options->View, scroll to the last item and make sure "Use Simple File Sharing" is unchecked. Apply changes if necessary.
1a. If you're using Windows XP Home Edition, you have two choices: you can either boot to Safe Mode so that the file Security Tab will be available to you, or you can download and install scesp4i.exe - which is the NT 4.0 Security Configuration Editor - and have the capability even in Normal Mode. I had to Google this file since it appears to be a dead link (at least for me, today) on MS' site. Download this file, unpack to a temporary directory, right-click the setup.inf file and pick install. After rebooting, the file Security Tab will be available to you in Normal Mode. Disclaimer: You do this at your own risk. I tried it on an installation of XP Home and it worked without issue. Make a System Restore point for insurance.
2. Navigate to the file or folder you wish to take ownership.
3. Right click the file/folder->Properties->Security and click the Advanced button.
4. Click the Owner Tab, highlight Administrators (this is my personal recommendation for owner), check the box that says "Replace owner on subcontainers and objects" (if present). Apply your changes.
5. Answer the subsequent prompt by clicking Yes. The files now belong to you.
If you still can't delete it, odds are very good that the file is in use. If this is the situation, you might first try removing the file in Safe Mode. In the event that the file is still in use, which is the case with some malware/virus type files, you can try using the application at http://www.snapfiles.com/get/moveonboot.html (originally brought to my attention by Will889). And, for information on what has your file locked (link provided by Mjölnir), you might try http://www.dr-hoiby.com/WhoLockMe/index.php.
Personally, if I get to the point that none of the above steps work, before I even get to the Safe Mode step, I remove the drive and connect it as a data drive on another machine and delete whatever files I want. It's possible that you will still have to take ownership during this step, but it's guaranteed to work.
On a related note, Windows 2000 and XP do not have a deltree command, per se, but the following command serves the same purpose and works quite well:As a bonus, this command works much faster than attempting the same thing using Explorer. One should take care when mass deleting files in this manner, since the /q switch bypasses the confirmation dialogue Windows would ordinarily present.Code:rd <directoryname> /s /q
Further, it should be noted that files deleted from a command prompt are much more difficult to recover than files deleted in Windows Explorer (assuming that you don't use shift+delete). To me, this is a bonus.
*If in doubt as to the wisdom of removing a file, you might Google the filename first to make sure it's not a sytem file (with Windows 2000 or XP, hopefully Windows File Protection will come to the rescue) or is not an integral part of some memory resident software like a software firewall or antivirus program. Additional resources (courtesy of Mjölnir) include, but are not limited to: