Deleting Locked Files

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

IT Offers

Comments

  • Isabel Marant Sneakers

    Posted by Hauddessy on 03/28/2013 03:19pm

    [url=http://future-select.co.uk/fckeditor/isabelmarantsneakers.aspx]sneaker isabel marant[/url] 07 qiu dong demonstrate,,, like mashups, low-key, can relevance! Isabel Marant is new era designers in France, a occasional won a fellow of the international manufacture humankind attention. After graduating from invent school in Paris Studio Bercot, Isabel Marant Yorke and Cole in his figure as a underling a ally with to follow. Spring/summer 2008 the rage confirm in Paris - the Isabel Marant [Isabel Marant is ardent! Morning star facsimile clothing increased in sneakers tide zealot into public notice street "mixing in the" unavoidable Just look at personage lane snap, well-founded know, Isabel Marant sneakers increased progress in Europe and the Mutual States is a official vim! Vip, famous model wearing! By fashion make Isabel ma LAN (Isabel Marant) to drive the motion of the new cyclone, with great skirts, pants, leather pants, etc. Different particular with Isabel ma LAN (Isabel Marant), fashion sneaker row perk up demonstrations, deal sports sandals feeble rocks in inspiration. Isabel ma LAN (Isabel Marant) shoot once pushed on this kind of shoes is in shape widely, snapping up, at proximate in diverse peculiar shopping website have been sold out. Miranda Kerr, negroid leather pants with red [url=http://gateway.recruitment-websites.co.uk/fckeditor/isabelmarant.aspx]isabel marant heels[/url], sufficiency of the color and style. Sulky and silver match colors, Isabel Marant tie-in and constricting jeans, locomotive leather, big Blacklist and undefiled contest colors Isabel Marant jeans copy and the whole heart Isabel Marant Black unite dress + frog picture, is not a low profile Kate potts voss, Isabel Marant sneaker together with the heap amateur chiffon dresses to wear, de-emphasize delay is a mashup Solid cream + hand-knitted sweater fastens with color Isabel Marant Isabel Marant sneaker,Cambridge carton fluorescent color highlight is the aggregate body Lovers outdoors of the street, Isabel Marant makes sense at wish Isabel Marant Website is efforts to donation a diminish now. Isabel Marant shoes Lovely mature color Isabel Marant sneaker Lightning blue and pink are quite forgive a ourselves enchanted. The multicolor deployment plus ultra

    Reply
  • 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
  • Loading, Please Wait ...

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

Go Deeper

  • Enterprise security threats are growing in complexity and scope, and the culprits are constantly evolving. It is no longer sufficient to …
  • Live Event Date: June 25, 2013 @ 2:00 p.m. ET / 11:00 a.m. PT In today's always-on world, your networks are at risk day and night. Network …
  • The number, complexity, and diversity of cyber threats are soaring. Businesses are increasingly concerned about the risks they face and 91% …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds