DEV Community

Cover image for Why Windows wouldn't let me rename a folder I wasn't using
Andy Yeung
Andy Yeung

Posted on Originally published at amsyn.org

Why Windows wouldn't let me rename a folder I wasn't using

Background

I wanted to give a Next.js project folder a new name. I never thought that would take hours: every attempt ended with Access is denied, even though I had closed every file inside it.

Most people give up and reboot. I wanted to know what was actually holding it.

What I tried

  • Stopped the dev server: still couldn't rename it.
  • Closed every editor window: a quick check showed more than ten editor processes still running in the background.
  • Force-quit those editor processes: that also stopped another project's dev server running in its terminal, and the rename still failed.
  • Asked Windows Restart Manager about every file: over 800 files and not a single one was locked.
  • Installed PowerToys: its install log said the machine was waiting for a reboot.

In the end I kept the old name, but I finally understood what was going on.

Why Windows locks things

Windows makes a different trade-off from macOS and Linux:

  • Windows: when a program opens a file, other programs can't delete or rename it unless the first program explicitly allows it. That way nothing swaps out a file a running program depends on.
  • macOS and Linux: you can rename or delete a file while it's open, and the program that opened it keeps reading the old contents.

What gets locked is often the folder itself rather than any one file. In a development project the usual suspects are:

  • Working directory: once a terminal has cd'd into a folder, the folder can't be renamed.
  • File watchers: editors and dev servers watch the whole project, and they can keep running after the window is gone.
  • Antivirus scans: Defender scans newly written files, and node_modules holds tens of thousands of them.
  • System services: the search indexer and Explorer thumbnails and previews.

How to find the culprit

1. File Locksmith shows it in one click

After installing Microsoft's free PowerToys, right-click the folder and choose "What's using this file?". It lists the processes holding it and can end them for you. On Windows 11 you may need "Show more options" first.

2. No tools installed: Resource Monitor

It's built in: open Resource Monitor, go to the CPU tab and search for the folder name under Associated Handles.

3. From the command line: Sysinternals handle

handle.exe -nobanner my-project
Enter fullscreen mode Exit fullscreen mode

All three list the handles each process holds, folders included. Restart Manager only checks files, which is why it found nothing when the folder itself was locked.

Mistakes I made

  • Closing the windows isn't closing the program: with every editor window gone, more than ten processes were still running.
  • Force-quitting has side effects: a dev server running in the editor's terminal stops with it.
  • Using the wrong tool: a file-only check can't see folder locks, so my "nothing is locked" conclusion was wrong.
  • A pending reboot: unfinished system updates make every kind of lock harder to track down.

What improved

Before Now
Finding the culprit Closing apps one by one and guessing One right-click
Unlocking Rebooting Ending only the process that holds it
Prevention None Close the editor, dev server and terminals before renaming

Two more ways to prevent it: keep projects on a Windows 11 Dev Drive (or add them to Defender's exclusions), or run Node.js projects inside WSL.

Let AI apply it for you

If a file or folder won't rename for you, paste this into an AI coding assistant:

On Windows, renaming or deleting this folder fails with "Access is denied":
<folder>

1. Find which processes hold handles inside it, including directory handles and
   processes whose current directory is inside it (not only open files).
2. List each process with its name, command line and parent, and say what it is
   (editor, dev server, file watcher, antivirus, shell).
3. Suggest the least disruptive way to release each one (close a terminal, stop a
   dev server, quit an app) before any force-kill, and ask me before killing anything.
4. If nothing is found, check for a pending reboot and suggest PowerToys File Locksmith
   or Sysinternals handle as the next step.
Enter fullscreen mode Exit fullscreen mode

Cover: Closeup padlock wooden door by Markus Spiske, CC0 1.0.

Top comments (0)