The Chalkboard


Finding a process that's locking a file on Windows

Last Updated: [2023-04-06 Thu 05:20]

This page has been adapted from some internal notes which has had company senstive information stripped.

Introduction

Windows will refuse to delete a file if a process is using it (unlike Linux which will keep the file open but allow you to delete the reference to it). This can cause problems during a deployment via tools such as Octopus Deploy.

In the example above, we can see that during a deploy, we got the message "Access is denied" to a file when trying to purge a directory.

Looking at the file on Windows, we can see that the SYSTEM user has full access to that file. As the Octopus Deploy tentacle runs as the SYSTEM user, it's obvious this isn't a permissions issue. The next hunch would be that some process is holding the file.

Process Explorer

Unfortunately, Windows doesn't provide an easy way to check this. But again, we can rely on a tool by Microsoft's CTO, Mark Russinovich, called Process Explorer, which can be downloaded from Microsoft's website: https://docs.microsoft.com/en-gb/sysinternals/downloads/process-explorer

Process Explorer is a powerful tool, that some Sysadmins use as a drop-in replacement to Task Manager. For us we can use a feature to check for find open file handles.

To do this:

  1. Launch Process Explorer as Administrator.
  2. Go to Find → Find Handle or DLL…
  3. Insert the full path of the problematic file.

In the screenshot above, we can see the expected process has hold of the file. This documentation was written after fixing the issue, however at the time, the Windows Problem Reporting (WerFault.exe) process had the file open.

Clicking on the result will cause the main window of Process Explorer to focus on that process, where you can right-click on it and kill it to release the file.


DISCLAIMER: The information provided on this website is generated from my own notes and is provided "as is" and without warranties. Robert Ian Hawdon can not be held responsible for damages caused by following a guide published on this site. This website contains links to other third-party websites. Such links are provided as convienice of the reader. I do not endorce the contents of these third party sites.