sodcalculator

10 Tech How-To

How to Safely Remove node_modules on Windows

Guide 10-42399 · Updated · Step by step

Removing a project’s node_modules folder on Windows is often necessary to fix dependency issues, free up disk space, or start fresh. This guide walks you through the safest methods, including using built-in Windows tools and command-line utilities, to avoid file-locking errors and permission problems. You’ll learn how to delete the folder completely, even when standard Explorer deletion fails.

What You’ll Need

  • Administrator access to the computer (for some methods).
  • Node.js installed (to use npx or npm commands).
  • Your project folder path (e.g., C:\my-project).

Step-by-Step

  1. Close all related programs. Make sure no editor (VS Code, etc.), terminal, or any process that might be using files inside node_modules is open. This includes Node.js servers, build tools, or file watchers. If you’re unsure, close everything and restart your computer to release all file handles.
  2. Open Command Prompt as Administrator. Press Windows, type cmd, right-click “Command Prompt”, and select “Run as administrator”. Confirm any UAC prompt.
  3. Navigate to your project folder. In the command prompt, type: cd C:\path\to\your\project and press Enter. Replace the path with your actual project location.
  4. Use the built-in rmdir command. Type rmdir /s /q node_modules and press Enter. The /s flag removes all subdirectories and files, /q runs in quiet mode (no confirmation prompts). This often works when Explorer fails. Wait for the command to finish—it may take a few minutes.
  5. If rmdir fails, use PowerShell with Remove-Item. Open PowerShell as Administrator (right-click Start > Windows PowerShell (Admin)). Navigate to the project: cd C:\path\to\project. Then run: Remove-Item -Recurse -Force node_modules. The -Force flag overrides read-only attributes.
  6. If still stuck, use npx rimraf. In the same PowerShell or Command Prompt, run: npx rimraf node_modules. This uses the popular rimraf package (which mimics Unix rm -rf) and handles long paths and permission issues better. It will download the package temporarily if needed.
  7. Verify the folder is gone. Type dir to list the contents of your project folder. You should not see node_modules listed.

Common Mistakes

  • Deleting while a process is using the folder. This is the #1 cause of errors like “file in use”. Always close your editor, terminal, and any Node processes.
  • Using File Explorer to delete. Explorer often fails on deep paths or long file names, leaving partial folders behind. Use command-line tools instead.
  • Not running as Administrator. Some files have restricted permissions; without admin rights, deletion fails silently or partially.
  • Forgetting to reinstall dependencies. After removing node_modules, you must run npm install (or yarn) before the project works again.

Tips

  • Use a package manager script. Add a script to your package.json: "clean": "rimraf node_modules" to make future cleanups easier.
  • Consider using npm ci instead of npm install after cleanup. It installs dependencies exactly from the lock file, ensuring a clean, reproducible build.
  • If you have many projects, use a tool like npx npkill to find and delete all node_modules folders at once, but be careful—it will ask for confirmation.
  • Keep your Node.js and npm updated. Newer versions handle long paths better and have fewer permission quirks.

Troubleshooting

If the folder still won’t delete, try these in order:

  • Restart Windows in Safe Mode. This prevents most background processes from locking files. Boot into Safe Mode, then run rmdir /s /q node_modules from an admin Command Prompt.
  • Check for long path issues. Windows historically limited paths to 260 characters. Enable long paths via Group Policy or registry (search “Enable NTFS long paths” for your Windows version). Then retry deletion.
  • Use a third-party unlocker. Tools like “LockHunter” or “Unlocker” can show which process is locking a file and force-close it. Use them cautiously and only from trusted sources.
  • If you see “Source file names are larger than is supported by the file system”, rename the parent folder to a shorter name (e.g., proj) and try again.
  • As a last resort, format the drive. Only if the folder is on a secondary drive and you can back up other data. This is extreme and rarely needed.

Frequently Asked Questions

Is it safe to delete node_modules?

Yes. It contains only installed packages and can be recreated with npm install. It’s not part of your source code.

Why does Windows say “The action can’t be completed because the folder is open”?

This means a program (like your editor or a Node process) is using files inside. Close all programs, or restart your computer, then try again.

What’s the difference between rmdir and Remove-Item?

Both delete folders. rmdir is the classic command-line tool; Remove-Item is PowerShell’s cmdlet with more options. They are interchangeable for this purpose.

Do I need to reinstall all packages after deleting node_modules?

Yes. Run npm install or yarn to re-download all dependencies listed in package.json.

Can I delete node_modules while the project is running?

No. Stop any running server or build process first. Otherwise, you’ll get file-lock errors and may corrupt the running app.

Why is node_modules so huge?

It contains thousands of files from all your dependencies, including nested dependencies. It’s normal for it to be hundreds of megabytes or even gigabytes.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →