How to Delete node_modules - Deep Nested Folder in Windows

node.jsWindows 7Npm

node.js Problem Overview


Upon trying to remove the node_modules directory created by npm install:

> The source file name(s) are larger than is supported by the file > system. Try moving to a location which has a shorter path name, or try > renaming to shorter name(s) before attempting this operation

I also tried shift + delete and still having the same issue.

node.js Solutions


Solution 1 - node.js

Since this the top google result, this is what worked for me:

Update, if you have npm v5, use npx:

npx rimraf ./**/node_modules

Otherwise install RimRaf:

npm install rimraf -g

And in the project folder delete the node_modules folder with:

rimraf node_modules

If you want to recursively delete:

rimraf .\**\node_modules

[ http://www.nikola-breznjak.com/blog/nodejs/how-to-delete-node_modules-folder-on-windows-machine/ ]

Solution 2 - node.js

I've simply done that by using Winrar, this may seem a strange solution but working very well.

  • right click on node_modules folder
  • select Add to archive ... from the menu.
  • Winrar dialog opens
  • just check the option delete files after archiving
  • Don't forget to delete the node_modules.rar after finished.


[UPDATE] This also works with 7Zip

Solution 3 - node.js

DELETE only by using DOS command without any installation:

Create an empty folder "test" on C or D drive and use following DOS command

robocopy /MIR c:\test D:\UserData\FolderToDelete > NUL

After completing above command, your folder will be empty, now you can delete the folder.

Don't worry your test folder will always be empty, so you can delete it at any time.

Solution 4 - node.js

I used GitBash to remove de folder!

rm -r node_modules

It took a while to delete everything, but worked for me!

Solution 5 - node.js

You can use Git Bash to remove the folder:

example: c:\users\olefrank\projects\mynodeproject

> rm -rf /c/users/olefrank/projects/mynodeproject

Solution 6 - node.js

Try Visual Studio Code

After trying many solution i find this one is pretty simple. just open the project in Visual code and delete it. the UI may freeze for some seconds but it will definitely works.I test using many large size node_modules folder with it

enter image description here

Thanks

Solution 7 - node.js

Delete Deep Netsted Folder like node_modules in Windows

  1. Option 1

Delete using rimraf NPM package

  • Open command prompt and change your directory to the folder where node_modules folder exists.

  • Run

    `rimraf node_modules`
    
  • Missing rimraf ERROR then Install

     `npm install rimraf -g`
    
  • When the installation completes, run

      `rimraf node_modules`
    
  1. Option 2:

Detele without installing anything

  • Create a folder with name test in any Drive

    robocopy /MIR c:\test D:\UserData\FolderToDelete > NUL

  • delete the folder test and FolderToDelete as both are empty

Why this is an issue in windows?

One of the deep nested folder structure is node_modules, Windows can’t delete the folder as its name is too long. To solve this, Easy solution, install a node module RimRaf

Solution 8 - node.js

Please save yourself the need to read most of these answers and just use npx rather than trying to install rimraf globally. You can run a single command and always have the most recent version with none of the issues seen here.

npx rimraf ./**/node_modules

Solution 9 - node.js

I think this was not mentioned before. but the best way to delete unwanted node_modules is to install an utility called npmkill.

Installation:

From your terminal:

npm i -g npkill
And to use it:

From your terminal:

npkill

or alternatively, you can directly use it without installation by writing:

npx npkill

You will then be presented with a list of projects, and by hitting space bar you can delete their node_modules.

Solution 10 - node.js

You can use Git Bash to remove the folder:

example: c:\users\stu\projects\mynodeproject

> rm /c/users/stu/projects/mynodeproject -rfd

Solution 11 - node.js

simple just run for windows I haven't tested it for other operating systems

rm -r node_modules

in fact, you can delete any folder with this.

like rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete.

just open the gitbash move to root of the folder and run this command

Hope this will help.

Thanks, Ajay Kotnala

Solution 12 - node.js

I had a similar problem and RD didn't work, for some unknown reason.

NPM can get rid of its own mess though, so if you do npm uninstall [module-name] for each directory in node_modules, you'll get rid of them.

(I'll look up how to batch loop this later, for those who have lots of dependencies.)

Solution 13 - node.js

  1. npm install -g remove-node-modules
  2. cd to root and remove-node-modules
  3. or remove-node-modules path/to/folder

Source:

https://github.com/j-quelly/node-cleanup

Solution 14 - node.js

On Windows Platform the simplest way is to use the terminal.
Please Run the command RMDIR /Q/S foldername to delete the folder and all of its subfolders.

Example: RMDIR /Q/S node_modules

The Above Command deletes node_modules folder and its subfolders.
For information please visit this https://www.ghacks.net/2017/07/18/how-to-delete-large-folders-in-windows-super-fast/

Solution 15 - node.js

I just do del node_modules inside my project folder on PowerShell, it will ask you if you want to remove it and its children folder, just hit 'Y' and that's it

Solution 16 - node.js

On Windows, using Total Commander all you have to do is select the folder click shift + delete . Don't forget about the shift key.

Solution 17 - node.js

Any file manager allow to avoid such issues, e.g Far Manager

enter image description here

Solution 18 - node.js

Tried everything but didn't work. All the above methods did nothing. At last I was able to delete via VSCode.

  1. Just Open your root folder with VSCode.
  2. Select node_modules folder and delete.
  3. Profit. (It will take few milliseconds to delete.)

VSCode-delete.png

Solution 19 - node.js

I made a Windows context item to fast delete node_modules or other folders. I use it when Windows doesn't delete a folder because of some invalid chars in the directory path.

HOW TO INSTALL?

  1. Install rimraf => npm install rimraf -g

  2. Create a new file named delete.bat, set the content as below and copy it into c:\windows\system32\

delete.bat:

 @ECHO OFF
 ECHO.
 ECHO %CD% 
 ECHO.
 ECHO Are you sure to delete the folder with Rimraf?
 PAUSE
 SET FOLDER=%CD%
 CD /
 
 rimraf "%FOLDER%"
 
 rem DEL /F/Q/S "%FOLDER%" > NUL
 rem RMDIR /Q/S "%FOLDER%"
 
 EXIT

3. Run fast-delete.reg file to import into registry.

Done!

final result

Solution 20 - node.js

I needed to clean up an entire Projects directory for backup purposes, so I installed rimraf and ran this at the root dir (inside a git bash prompt):

find . -name "node_modules" -type d -prune -exec rimraf '{}' +

Very effective, truly recursive (avoids children node_modules) and fast on windows (thanks to rimraf).

Sources:

  1. https://rtmccormick.com/2018/01/10/clear-node-modules-folders-recursively-mac-linux/
  2. The accepted answer in this question that suggests rimraf but lacks in the recursive aspect

Solution 21 - node.js

This really worked for me:

npm i npkill

npx npkill

select with CURSORS and press SPACE key to delete

Solution 22 - node.js

Just use powershell..

Run powershell and cd to the parent folder and then:

rm [yourfolder]

as in:

rm node_modules

Solution 23 - node.js

One solution that I use:

(I would prefer to avoid installing a new extension (rimraf) when working with CI environments.)

  1. A) Rename packages.json to something else. B) Specially on CI - after npm install, I usually remove the file instead of renaming it, but if you need it, you don't have to do this. That's your choice.
  2. run npm init - this will create an empty packages.json file (no dependencies)
  3. run npm prune - this will match node_modules with the dependencies section of packages.json - which is now empty as the result of step #2.
  4. If you have chosen #1.A. step, delete the newly created packages.json, and rename original packages.json back to its original name.

Solution 24 - node.js

Its too easy.

Just delete all folders inside node_modules and then delete actual node_module folder.

This Works for me. Best luck....

Solution 25 - node.js

Sometimes, even if you install rimraf globally you can have a local rimraf dependency (SASS usually have it). In this case I would run following commands:

Follow first 2 steps as usagidon recommended, if you have issues or errors try

npm uninstall rimraf & rimraf node_modules

this will delete local rimraf and use the global one

Solution 26 - node.js

On Windows my go to solution is using the rmdir command:

rd /S .\node_modules\

If it fails the first time -- try one more time. Also check if you have running scripts currently using the modules (npm run serve or similar).

Solution 27 - node.js

The PowerShell way:

PS > rm -r -force node_modules

# The same, but without using aliases
PS > Remove-Item -Recurse -Force node_modules

And if you want to delete every node_modules in sub directories:

Note Potentially dangerous as it deletes recursively, be sure of what you're doing here

PS > dir -Path . -Filter node_modules -recurse | foreach {echo $_.fullname; rm -r -Force $_.fullname}

Solution 28 - node.js

From this looks of this MSDN article, it looks like you can now bypass the MAX_PATH restriction in Windows 10 v1607 (AKA 'anniversary update') by changing a value in the registry - or via Group Policy

Solution 29 - node.js

I'm on windows 10 and I could'nt delete folders with message "directory not emtpy". Neither rimraf nor rm -rf worked.

Copying an empty text file to every single folder did the trick - I was able to delete the complete node_modules folder.

Solution 30 - node.js

Not exactly related, but as this is the first post I found in my search for a similar solution I think it's worth posting here.

I was running into permission issues on Windows 10 trying to delete my node_modules folder after a failed attempt at installing electron-redux. It seems electron-redux added @types to my node_modules, which had incorrect permissions set up.

rimraf did not work as it still ran into permission issues.

I ended up renaming node_modules then a simple delete worked.

Solution 31 - node.js

Okay so my problem was that i had .bin folder inside node_modules and i wasn't able to delete that, I tried all the above solutions for the same but nothing worked.

Error which was repeating was "u do not have permission to delete the folder or its been used by some other program".

Solution that i found "I had Antivirus installed on my pc ,which was unknowingly using the files from node_modules folder".

I uninstalled the antivirus or u can change permission setting from antivirus setting option (depends on which antivirus u have).Now i am able to delete node_modules folder.

Solution 32 - node.js

Advance a little beat for multiple folders...

If you have a project folder with multiple 'builds' that contains 'node_module'. Like:

C:/Project/build-1/node_modules
C:/Project/build-2/node_modules
C:/Project/build-3/node_modules
C:/Project/build-4/node_modules

You want to delete all folders starts with "build"

! First install nodejs and rimraf as suggested above !

Then go to path with project and run PowerShell command

cd  C:/Project/
Get-ChildItem build* | foreach {rimraf -Path $_.FullName}

It will take a wile

Solution 33 - node.js

What worked for me was:

  1. closed the node manager console

  2. closed the Atom (visual studio code ) dev environment you are in.

  3. then delete the node_modules

     npm install rimraf -g 
     rimraf node_modules
    

Solution 34 - node.js

You may use another npm package specifically for this purpose. By installing it locally in the folder using

npm i rm-node-modules-recur

If you wish you can also install it globally with -g switch.

If installed locally you can invoke the binary from current directory node_modules.bin\ using

 .\node_modules\.bin\rmnodes "<top level directory within quotes>"

If you chose global installation simply use

rmnodes "<top level directory within quotes>"

and thats it .. this package will end up deleting node_modules from all sub directories recursively (in my case some 60 odd folders) with ease. refer attached snapshot. using rm-node-modules-recur

Solution 35 - node.js

You can make simple batch based on @mike-caron answer so you don't need to type every time whole robocopy command instead just input path to selected folder:

@echo off
ECHO What Directory would you like to empty?
ECHO Current path: %cd%
SET /p UserInputPath=Input relative path to directory:
ROBOCOPY /MIR empty_dir %cd%\%UserInputPath% > NUL
PAUSE

Here you are using empty directory named empty_dir in robocopy command that needs to be in same directory with batch file for this to work. After batch file finishes its job both selected directory and empty_dir directory will be empty so that you can remove them.

I made a simple batch file that creates empty folder and after robocopy command is executed removes both empty folder and selected folder so that only thing you need to do is enter path to selected folder that you want to delete. It is fast and practical if you don't want to install stuff like rimraf. You can download it here https://github.com/5imun/WinCleaner

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleohView Question on Stackoverflow
Solution 1 - node.jsblubView Answer on Stackoverflow
Solution 2 - node.jsEymen ElkumView Answer on Stackoverflow
Solution 3 - node.jsAli AdraviView Answer on Stackoverflow
Solution 4 - node.jsBrunoView Answer on Stackoverflow
Solution 5 - node.jsolefrankView Answer on Stackoverflow
Solution 6 - node.jsUmar AbbasView Answer on Stackoverflow
Solution 7 - node.jsJinna BaluView Answer on Stackoverflow
Solution 8 - node.jsArtif3xView Answer on Stackoverflow
Solution 9 - node.jsakmurView Answer on Stackoverflow
Solution 10 - node.jsStuart.SklinarView Answer on Stackoverflow
Solution 11 - node.jsAjay KotnalaView Answer on Stackoverflow
Solution 12 - node.jsChrisView Answer on Stackoverflow
Solution 13 - node.jsj_quellyView Answer on Stackoverflow
Solution 14 - node.jsWoriayibapri Hearty AlapherView Answer on Stackoverflow
Solution 15 - node.jsRoy.BView Answer on Stackoverflow
Solution 16 - node.jsAndrzej GisView Answer on Stackoverflow
Solution 17 - node.jsVeniaminView Answer on Stackoverflow
Solution 18 - node.jsJunaid AtariView Answer on Stackoverflow
Solution 19 - node.jsAlper EbicogluView Answer on Stackoverflow
Solution 20 - node.jsLino MediavillaView Answer on Stackoverflow
Solution 21 - node.jsbijayadhsView Answer on Stackoverflow
Solution 22 - node.jsDarren FordView Answer on Stackoverflow
Solution 23 - node.jsRikkiView Answer on Stackoverflow
Solution 24 - node.jsPriyankaView Answer on Stackoverflow
Solution 25 - node.jsuser2869931View Answer on Stackoverflow
Solution 26 - node.jskorunView Answer on Stackoverflow
Solution 27 - node.jsdgellowView Answer on Stackoverflow
Solution 28 - node.jsmolokoView Answer on Stackoverflow
Solution 29 - node.jsralusnomView Answer on Stackoverflow
Solution 30 - node.jsJake ConniffView Answer on Stackoverflow
Solution 31 - node.jsgaurav bhattView Answer on Stackoverflow
Solution 32 - node.jsAidar GatinView Answer on Stackoverflow
Solution 33 - node.jsSumanthView Answer on Stackoverflow
Solution 34 - node.jsManeesh PariharView Answer on Stackoverflow
Solution 35 - node.jsŠimeView Answer on Stackoverflow