355

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.

Jinna Balu
  • 6,747
  • 38
  • 47
leoh
  • 10,378
  • 7
  • 28
  • 39
  • `try renaming to shorter name(s) before attempting this operation` – SLaks Jan 27 '15 at 16:30
  • 3
    Thanks for your suggestion,@SLaks! I should have mentioned that I tried that too, but the files and dirs are many and deep, it has things like **C:\Users\my_user_name\projs\backend\node_modules\imagemin\node_modules\imagemin-optipng\node_modules\optipng-bin\node_modules\bin-wrapper\node_modules\bin-check\node_modules\executable\node_modules\meow\node_modules\camelcase-keys\node_modules\...** and it goes on. It's impractical to rename files manually and I hope there will be easier ways without writing a program – leoh Jan 27 '15 at 16:47
  • 3
    Webstorm can delete all. – ozzimpact Dec 22 '15 at 11:54
  • I used the Long Path Fixer tool and successfully removed this ".meteor" folder. (https://long-path-fixer.en.softonic.com/) – Doan Vu Aug 02 '18 at 05:16
  • RUN INSTALL, npm install rimraf -g, RUN DELETE, rimraf node_modules – Jinna Balu Aug 06 '18 at 06:38
  • Npkill is the best option as of 3/2020. npm i -g npkill , then run via: npkill – ZStoneDPM Mar 04 '20 at 21:50
  • Use `npx npkill` to delete all node_modules in a folder. It will list all of the subfolders containing node_modules. You can select using cursor and space to delete for a particular folder – Mohit kumar Jun 12 '21 at 14:59

39 Answers39

679

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

Update, if you have npm v5, use npx:

npx rimraf --glob **/node_modules

Otherwise install RimRaf:

npm install rimraf -g

And in the project folder delete the node_modules folder with:

rimraf --glob node_modules

If you want to recursively delete:

rimraf --glob .\**\node_modules

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

blub
  • 8,757
  • 4
  • 27
  • 38
  • not working here: "Error: ENOTEMPTY, directory not empty". and supplied link not working! – olefrank Jun 12 '15 at 08:04
  • 20
    I'm the author of the blog post. @olefrank: this tends to happen when you try to delete the whole folder (not just node_modules), and also if you happen to have the folder opened in an Explorer window. usagidon, thanks for linking the original source. – Nikola Jul 23 '15 at 13:33
  • 2
    That won't be useful if you don't have NodeJS, or you're trying to delete the global modules of NodeJS after uninstalling it (that is why I had to use robocopy) – Zorgatone Mar 02 '16 at 12:45
  • You can delete anz other folder with rimraf, not only node_modules – Amiga500 May 12 '16 at 08:32
  • This only works if rimraf is not a (transient) dependency of your project. Otherwise there will be an error: The system cannot find the path specified. Any solution for that? – Mario Eis Oct 02 '17 at 10:51
  • 3
    I'll add my 2 cents here to be a bit more specific to the question. You can delete all the node_modules folders recursively (using globbing pattern) like this: `rimraf .\**\node_modules` – Artif3x Mar 01 '19 at 15:22
  • Just a note: on Windows 10 I got an error, then ran rimraf in an admin powershell and every worked! Great answer! – jeffski13 Aug 10 '19 at 17:14
  • 4
    One-liner: `npx rimraf node_modules`. Npx is included with NPM 5.2+ and is a eXecutor for npm packages, which automatically downloads missing npm packages. – andy Nov 18 '19 at 12:42
  • 1
    @Artif3x 's commnent should be marked as the best answer loll! thanks for the tip for deleting recursively :-) – Juan Stoppa Dec 24 '19 at 01:09
  • `rimraf ./**/node_modules` when running on linux or macs – shandoosheri Aug 02 '20 at 00:46
  • `rimraf` didn't work for me, even as Administrator. Same goes for many of the answers (`robocopy`, `rmdir`, Winrar, Visual Studio Code, etc.) below. What did work was renaming folders along the problem path to much shorter names. `node_modules` became `n`, `my-long-project-name` became `m', and so on. I then deleted the `n` folder with no problems. Finally, I chose a Linux distribution to develop under forever more. – Mark Oct 01 '21 at 15:19
  • This post is old, but still helpful. Maybe somebody knows for certain, but it seems like the glob pattern results in a recursive search for "node_modules" within node_modules which can take quite some time. Is this the case? If so, is there a way to skip searching in node_modules? – webprojohn May 25 '22 at 13:39
  • 2
    Updates: I am getting an "Error: Illegal characters in path" via `rimraf .\**\node_modules` with the latest version. It confused me and I've just got an answer to resolve it: https://stackoverflow.com/questions/75281066/error-illegal-characters-in-path-in-npm-rimraf/75309179#75309179 – Jeff Mar 16 '23 at 16:20
133

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

Eymen Elkum
  • 3,013
  • 1
  • 20
  • 34
91

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.

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
  • 14
    best answer, because you don´t need to install anything – andyrandy Dec 20 '15 at 11:33
  • 1
    After trying rimraf and adding a network folder, this is the cleanest solution I've found. Thanks for sharing this. – danyim Feb 11 '16 at 22:21
  • 1
    great solution; easy. – bob.mazzo May 16 '16 at 15:15
  • 2
    I just made a minor addion: `> NUL`. When I just tried this, it was being throttle by the console, and was going to take 15 minutes or more. With the redirection in place, it finished in about 2 – Mike Caron Nov 03 '16 at 19:15
  • 2
    It's funny that in order to delete some JS files, you need yet another JS library. – linuxunil Jul 23 '18 at 08:39
  • 1
    +1 for this (also I upvoted). I had issues with the rimraf approach due to getting permission denied on a few files under `node_modules/.bin`. The robocopy approach worked, when nothing else would. – sworisbreathing Aug 13 '19 at 06:42
72

I used GitBash to remove de folder!

rm -r node_modules

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

Bruno
  • 2,889
  • 1
  • 18
  • 25
43

You can use Git Bash to remove the folder:

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

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

olefrank
  • 6,452
  • 14
  • 65
  • 90
  • 9
    This did not work on Windows 7. Directory not empty error – Miguel Stevens Sep 02 '15 at 19:11
  • 5
    You need to specify the -d flag also, that will also delete directories. – Stuart.Sklinar Apr 20 '16 at 14:28
  • or -fR to recursively climb to the top and delete the folder as well – Dan Kanze Jul 28 '16 at 17:51
  • @Karthiga -rf are flags/options/parameters that tell the RM how to perform the delete operation. `r` says to recurse into any subdirectories and `f` says to force deletion (don't prompt). `-rf` is a unix convention to specify multiple flags/options at once. – Tim Coker Feb 19 '19 at 19:40
23

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

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Umar Abbas
  • 4,041
  • 2
  • 23
  • 21
23

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
Artif3x
  • 4,391
  • 1
  • 28
  • 24
22

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

  2. 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

Jinna Balu
  • 6,747
  • 38
  • 47
21

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.

akmur
  • 1,465
  • 2
  • 24
  • 37
12

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

Ajay Kotnala
  • 985
  • 9
  • 15
10

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/

6

You can use Git Bash to remove the folder:

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

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

Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89
5

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.)

Chris
  • 5,876
  • 3
  • 43
  • 69
5
  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

j_quelly
  • 1,399
  • 4
  • 16
  • 37
5

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

Roy.B
  • 2,076
  • 14
  • 23
  • Simple one. Thank you – estin sunny koothoor Oct 01 '18 at 04:22
  • ...\node_modules has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y rd : Directory C:\...\node_modules\ngx-color-picker\dist cannot be removed because it is not empty. rd node_modules -recurse – AUSTX_RJL May 20 '19 at 14:30
  • 1
    rd node_modules -recurse – AUSTX_RJL May 20 '19 at 14:36
  • @AUSTX_RJL rd also works, but I never had a problem with del node_modules.. wired, but tnx any way for another way of deleting in PS – Roy.B May 20 '19 at 14:47
3

Just use powershell..

Run powershell and cd to the parent folder and then:

rm [yourfolder]

as in:

rm node_modules
Darren Ford
  • 251
  • 3
  • 13
3

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

Junaid Atari
  • 533
  • 7
  • 17
3

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}
dgellow
  • 692
  • 1
  • 11
  • 18
2

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

leoh
  • 10,378
  • 7
  • 28
  • 39
Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
2

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

enter image description here

Veniamin
  • 466
  • 6
  • 18
2

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
  1. Run fast-delete.reg file to import into registry.

    Done!

final result

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
2

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
1

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.
Rikki
  • 3,338
  • 1
  • 22
  • 34
1

Its too easy.

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

This Works for me. Best luck....

Priyanka
  • 19
  • 1
  • Honestly, this is the faster way but a lot of patience is required. Even better if you delete a few folders at a time. – Santosh Jan 29 '20 at 05:59
1

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

user2869931
  • 47
  • 1
  • 12
1

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).

korun
  • 2,211
  • 1
  • 16
  • 12
1

I always use the GitBash for this purpose.

Run the below command in GitBash and it will delete node_modules folder very faster.

rm -rf node_modules
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
0

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

moloko
  • 423
  • 4
  • 8
0

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.

ralusnom
  • 135
  • 1
  • 8
0

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.

0

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.

gaurav bhatt
  • 23
  • 2
  • 12
0

On Windows

Run below to remove files under node_modules and sub directories & also remove folders in it:

DEL /F/S/Q node_modules > NUL 2>&1  &  RMDIR /S/Q node_modules > NUL 2>&1
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
0

Use this with CMD

npx rimraf node_modules
Sandeep Sherpur
  • 2,418
  • 25
  • 27
0

Latest version of rimraf doesn't work with double asterisk (i.e. rimraf .**\node_modules). Solution is to use older version:

npm i rimraf@3.0.2 -g

and then run

rimraf .\**\node_modules
Meeting Attender
  • 826
  • 11
  • 17
0

For windows users

> cd work_directory 
> FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

For linux users

$ cd work_directory 
$ find . -name "node_modules" -type d -prune | xargs du -chs

this will help to delete all deep nested node_modules in your directory

-1

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

Aidar Gatin
  • 755
  • 1
  • 7
  • 9
-1

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
    
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Sumanth
  • 477
  • 1
  • 6
  • 11
-1

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

Maneesh Parihar
  • 1,395
  • 2
  • 8
  • 6
-2

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

Šime
  • 139
  • 6