Deleting Locked Files
Posted
by Zoltan Csizmadia
on November 14th, 2000
This is a command line utility to close and delete a file which is locked by another process. I doesn't work with modules.
Usage
FORCEDEL.EXE [/S] filename /S Soft delete. Like the "del" command filename File name you want to delete
How does it work?
- Query the used file handles (system wide), and search for the processes which are using the file we want to delete.
- Start a remote thread (CreateRemoteThread) to close the given handle in every found process (#1)
For more information, check out the CodeGuru article entitled, Examine Information on Windows NT System Level Primitives.
Code
The following code closes a handle in a remote process. The handle must be remote process specific./* Note: 1. SE_DEBUG privilege must be enabled. 2. The function works with every kind of HANDLE 3. It will bother the remote process :) 4. The handles will be invalid after you closed them remotely */ //Close a handle in a remote process DWORD CloseRemoteHandle( DWORD processID, HANDLE handle ) { HANDLE ht = 0; DWORD rc = 0; _tprintf( _T("Closing handle in process #%d ... "), processID ); // open the process HANDLE hProcess = OpenProcess( PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, processID ); if ( hProcess == NULL ) { rc = GetLastError(); _tprintf( _T("OpenProcess() failed\n") ); return rc; } // load kernel32.dll HMODULE hKernel32 = LoadLibrary( _T("kernel32.dll") ); // CreateRemoteThread() ht = CreateRemoteThread( hProcess, 0, 0, (DWORD(__stdcall *)(void*))GetProcAddress(hKernel32,"CloseHandle"), handle, 0, &rc ); if ( ht == NULL ) { //Something is wrong with the privileges, //or the process doesn't like us rc = GetLastError(); _tprintf( _T("CreateRemoteThread() failed\n") ); goto cleanup; } switch ( WaitForSingleObject( ht, 2000 ) ) { case WAIT_OBJECT_0: //Well done rc = 0; _tprintf( _T("Ok\n"), rc ); break; default: //Oooops, shouldn't be here rc = GetLastError(); _tprintf( _T("WaitForSingleObject() failed\n") ); goto cleanup; break; } cleanup: //Closes the remote thread handle CloseHandle( ht ); //Free up the kernel32.dll if ( hKernel32 != NULL) FreeLibrary( hKernel32 ); //Close the process handle CloseHandle( hProcess ); return rc; }

Comments
delete file
Posted by KenRayn on 02/13/2012 06:35pmwell you can use a program for this like LongPathTool.com maybe ? :)
ReplyMissing Files
Posted by pislas on 09/11/2008 03:33pmSome of the files are missing from this download...cant make it compile, and it doesnt delete my in use files when i run it on them... ForceDel.exe "testfile.txt" SystemProcessInformation::Refresh() failed. Couldn't delete. Error = 32
ReplyCouldn't delete. Error = 5
Posted by glenneroo on 02/18/2007 02:15amSystemProcessInformation::Refresh() failed. Couldn't delete. Error = 5 when deleting this odd file that's from my old windows. It's the only file left before i can reformat this drive, but Vista won't let me :( D:\WINDOWS\system32\Macromed\Flash\Flash9.ocx any ideas?
ReplyThanks Zoltan!
Posted by acix on 02/28/2006 09:09amHow to open a process ??
Posted by elmajdouli on 01/09/2005 10:46amIn case our programme is running in simple user environnement. (A user wich don't have any power)
ReplyCouldn't delete. Error = 123
Posted by alauzon on 07/01/2004 09:35amTh efile I want to save is an e-mule preview avi file. I rebooted and I am still not able to delete. I am on Windows XP last version. Couldn't delete. Error = 123
ReplyYou are Life Saver !!!
Posted by Legacy on 12/30/2003 12:00amOriginally posted by: BiGBoY
AWESOME!!!! What more can i say?!
ReplyCouldn't delete. Error = 32
Posted by Legacy on 11/24/2003 12:00amOriginally posted by: Tony Lau
It works for me for many cases, but I got the following error message:
SystemProcessInformation::Refresh() failed.
Couldn't delete. Error = 32
from my server running Windows 2003:
OS Name Microsoft(R) Windows(R) Server 2003, Enterprise Edition
Version 5.2.3790 Build 3790
Any clues?
-
Reply
-
-
-
Replypublish the exe with changes
Posted by Dhino on 10/09/2007 03:15amMy answer
Posted by Feghoot on 06/05/2006 01:28pmI don't know if this is your problem, but my problem was that in SystemInfo.cpp he was only allocating 64K for the system process information, and it apparently wasn't enough. I changed BufferSize in the header file to 1M (0x100000), changed his allocation and free code to use malloc and free, and it worked. I also took out the EnableDebugPriv call because it seemed to leave mdm.exe pegging my processor, and added a call ::SetFileAttributes (lpFileName, FILE_ATTRIBUTE_NORMAL) before the call to ::DeleteFile, to handle read-only files.
Replyanswer?
Posted by fquenzer on 05/17/2006 04:50pmDid anyone by any chance have an answer to this question? I am getting this as well.
ReplyERROR CODE 32 in the Destructor code
Posted by judson_vc on 10/23/2005 07:20amhello ,i too got a same error. what i have to do for the ERROR =32. just i wrotea code that in a destructor { filename.close(); delete(filename); delete bmp; } in that code when i debug,the cursor goes but the GetLastError displays eror code=32 why so pls do reply to me folks
ReplyWorked after deactivating file index feature of XP
Posted by Legacy on 10/15/2003 12:00amOriginally posted by: Simon Smith
I had to deactivate the file index feature of XP (in the options of the file), then it worked.
ReplyErrors at first then success!
Posted by Legacy on 09/10/2003 12:00amOriginally posted by: Rich
ReplyLoading, Please Wait ...