Last Update - 10/04/2005

Tested on:
Windows NT Server
Windows 2000 Server
 

Sometimes we are unable to delete a file because it's been locked by another process which may have crashed or otherwise terminated without unlocking the file.

The error messages:
"There has been a sharing violation."
"The source or destination file may be in use."
Are commonly seen in these situations.

The simple solution to this problem is to reboot the server, all file locks are freed and the file can be moved, renamed or deleted without any problems. However, in a live environment, server reboots are not always acceptable. That leaves us with the tiny window on Server Manager that lists all the file locks, not much use.

Here's a way to free that file and leave the server up and running.
 
 

First, log on to the server where the file is located. (You'll need admin rights over this machine.)
 
At the command prompt, type:

   NET FILE
  

 
The command will produce a list like this:
ID         Path                                       User name             # Locks             

-----------------------------------------------------------------------------------
18         F:\Backup\Mailbox.pst                      ADMINISTRATOR         0
40         C:\WINNT\Profiles\All Users                ADMINISTRATOR         0
85         E:\Userdata\RES_CLERK_1\Templates\Memo.dot RES_CLERK_1           1
89         E:\Userdata\RES_CLERK_1\Mailbox.pst        RES_CLERK_1           1
  
We can see the files with ID numbers and the name of the user that is locking that file.
 
If the list is huge (and it probably will be on a live server), type:

   NET FILE > InUse.txt
  
to redirect the output to a text file.
 
Find the offending file and note the ID number. This is easier when using the text file method as we can use a text editor such as notepad to search through the output.
 
Type:

    NET FILE (FileIDNumber)
  
Where (FileIDNumber) is the problem file's ID. This will return the name of the file to confirm we have the right target.
 
Finally, type:

    NET FILE (FileIDNumber) /CLOSE
  
This should free the lock on the specified file.
 

Note: This should only be used against files locked on servers by network clients.

 

Problems, ideas, better routines ?
Mail me or drop into the forums


Home EMail Forum