Deleting Locked Files

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: Windows NT4 SP6, Windows 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?

  1. Query the used file handles (system wide), and search for the processes which are using the file we want to delete.
  2. For more information, check out the CodeGuru article entitled, Examine Information on Windows NT System Level Primitives.

  3. Start a remote thread (CreateRemoteThread) to close the given handle in every found process (#1)

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


Downloads

Comments

  • File not found ?

    Posted by james on 06/27/2012 01:25am

    hallo, if I want to build the downloaded project there is missed the "SystemInfo.h"-file. Is this a class built by you or is this a WIN-SDK-File ? best regards james

    Reply
  • delete file

    Posted by KenRayn on 02/13/2012 06:35pm

    well you can use a program for this like LongPathTool.com maybe ? :)

    Reply
  • Missing Files

    Posted by pislas on 09/11/2008 03:33pm

    Some 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

    Reply
  • Couldn't delete. Error = 5

    Posted by glenneroo on 02/18/2007 02:15am

    SystemProcessInformation::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?

    Reply
  • Thanks Zoltan!

    Posted by acix on 02/28/2006 09:09am

    this is exactly what I was looking for. Thank you very much!

    Reply
  • How to open a process ??

    Posted by elmajdouli on 01/09/2005 10:46am

    In case our programme is running in simple user environnement. (A user wich don't have any power)

    Reply
  • Couldn't delete. Error = 123

    Posted by alauzon on 07/01/2004 09:35am

    Th 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

    Reply
  • You are Life Saver !!!

    Posted by Legacy on 12/30/2003 12:00am

    Originally posted by: BiGBoY

    AWESOME!!!! What more can i say?!

    Reply
  • Couldn't delete. Error = 32

    Posted by Legacy on 11/24/2003 12:00am

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

    • publish the exe with changes

      Posted by Dhino on 10/09/2007 03:15am

      do u have a compiled exe with the changes above effected? if yes, pls publish.

      Reply
    • My answer

      Posted by Feghoot on 06/05/2006 01:28pm

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

      Reply
    • answer?

      Posted by fquenzer on 05/17/2006 04:50pm

      Did anyone by any chance have an answer to this question? I am getting this as well.

      Reply
    • ERROR CODE 32 in the Destructor code

      Posted by judson_vc on 10/23/2005 07:20am

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

      Reply
    Reply
  • Worked after deactivating file index feature of XP

    Posted by Legacy on 10/15/2003 12:00am

    Originally posted by: Simon Smith

    I had to deactivate the file index feature of XP (in the options of the file), then it worked.


    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date