318

I have multiple npm projects saved in a local directory. Now I want to take backup of my projects without the node_modules folder, as it is taking a lot of space and can also be retrieved any time using npm install.

So, what would be a solution to delete all node_modules folders recursively from a specified path using the command line interface?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumit
  • 3,652
  • 4
  • 13
  • 19

12 Answers12

724

Print out a list of directories to be deleted:

find . -name 'node_modules' -type d -prune

Delete directories from the current working directory:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

Alternatively you can use trash (brew install trash) for staged deletion:

find . -name node_modules -type d -prune -exec trash {} +
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darius
  • 10,762
  • 2
  • 29
  • 50
  • 70
    `-prune` is an important optimization. It'll case find not to recurse into `node_module` directories (to look for nested node_modules) – rzymek Jun 08 '17 at 14:35
  • 2
    For a project, I get `/node_modules/gulp-server-livereload/node_modules: Directory not empty` in a lot of "inner" node_modules folders. How to workaround this? – josemigallas Aug 19 '18 at 21:41
  • 70
    This is like, I'm serious, the 20th time I am at this answer to copy this code... xD – Cipi Aug 29 '18 at 14:55
  • 15
    What is the meaning of `'{}' +`? – Rokit Jul 25 '19 at 19:22
  • 41
    `{}` is a placeholder which `find` replaces with the file path it found. `+` tells `find` to append all the file paths to a single command rather than running `rm` for each. – Tamlyn Nov 22 '19 at 15:41
  • 2
    `find . -name 'node_modules' -type d -prune -exec du -chs {} + | grep t` to know the total size of what you are going to delete – Mikel May 05 '21 at 17:05
  • 2
    Seeing `rm -rf` in amongst any copy and paste command line makes me shudder. If the script doesn't find anything, or find isn't installed in your version of unix, and you're executing from root of your hard drive, it could delete your whole hard drive. Very unlikely, but hey, worth mentioning. – martinedwards Jun 07 '21 at 11:01
  • @Cipi I went to upvote your comment, but I have noticed I have already upvoted it, probably years ago :). – Tomáš Kafka Jul 07 '21 at 20:53
  • Why all put `-f` for `rm` each time? It's useless almost forever and here as well – vihtor Feb 13 '22 at 16:02
  • If you had a penny for every time I came to this post, You'd have enuff to buy some folks some fancy coffee on a given morning :) – Jason J. Nathan Aug 08 '22 at 05:12
  • Why not simply use `rm -rf node_modules **/node_modules`? – Simon Tran Oct 12 '22 at 18:27
237

Try https://github.com/voidcosmos/npkill

npx npkill

it will find all node_modules and let you remove them selectively.

npkill

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
jeckep
  • 2,972
  • 1
  • 14
  • 10
  • 12
    A better option than above since I usually have 2-3 projects which I want to keep node_modules intact – Black Mamba Jul 31 '21 at 05:04
  • 1
    This should be the top comment! – nkhil Oct 18 '21 at 17:54
  • 9
    I prefer to nuke all folders and reinstall them again as needed. Who has time to pick and choose manually? – fregante Nov 09 '21 at 23:41
  • 2
    @fregante Me. I dont have the time to wait for 10+ package.jsons with pre and postscripts to finish. This takes a lot longer than to think 20s and choose 10s. – aProgger Aug 07 '22 at 09:22
98

Improving on the accepted answer:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

I found that the command above would run a very long time to fetch all folders and then run a delete command. To make the command resumable, I'd suggest using \;. To see progress of the command being run, use -print to see the directory being deleted.

Note: You must first cd into the root directory and then run the command. Or instead of find ., use find {project_directory}.

To delete folders one by one:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' \;

To delete folders one by one and print the folder being deleted:

find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

For the people who like an interactive way of doing this, refer to jeckep's answer. Run this in the directory you wish to prune:

npx npkill
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sidharth
  • 1,675
  • 1
  • 15
  • 23
  • 3
    Nice improvements on the accepted answer @Sidharth, much appreciated – nclarx Aug 11 '19 at 06:14
  • Good job @Sidharth – dipenparmar12 Feb 09 '21 at 07:43
  • For (my own) future reference - I wanted to remove the contents of node_modules but leaving the directories in place (to add them to a Dropbox ignore list later). This works for me: `find . -name 'node_modules' -type d -prune -print -exec bash -c 'rm -rf "$0"/* "$0"/..?* "$0"/.[!.]*' {} \;` The wildcards are for matching hidden files and folders (starting with .) as per https://unix.stackexchange.com/a/77313/486273 – Jonáš Jančařík Aug 24 '21 at 08:49
  • Improving on this answer, using luminS ( https://github.com/wchang22/LuminS ) – `find . -name 'node_modules' -type d -prune -exec lms rm '{}' +` – Vinayak Kulkarni Sep 25 '21 at 13:44
  • bruh! this does wonders – codingbruh Apr 18 '22 at 16:14
13

I have come across with this solution,

  • first find the folder using find and specify name of the folder.
  • execute delete command recursively -exec rm -rf '{}' +

run the following command to delete folders recursively

find /path -type d -name "node_modules" -exec rm -rf '{}' +

Sumit
  • 3,652
  • 4
  • 13
  • 19
13

When on Windows, I use the following .BAT file to delete node_modules recursively from the current folder:

@for /d /r . %d in (node_modules) do @if exist %d (echo %d && rd %d /s /q) 

Or, via CMD.EXE:

>cmd.exe /c "@for /d /r . %d in (node_modules) do @if exist %d (echo "%d" && rd "%d" /s /q)""
raj varsani
  • 3
  • 1
  • 3
noseratio
  • 59,932
  • 34
  • 208
  • 486
7

bash function to remove node_modules. It will remove all node_modules directories recursively from the current working directory, while printing found paths.

You just need to put in somewhere in your $PATH

rmnodemodules(){

  find . -name 'node_modules' -type d -prune -exec echo '{}' \; -exec rm -rf {} \; 

}
Ivan V.
  • 7,593
  • 2
  • 36
  • 53
5

In Bash, you can simply use

rm -rf node_modules **/node_modules
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon Tran
  • 1,461
  • 1
  • 11
  • 28
3

If you want to move instead of delete it:

find . -name 'node_modules' -type d -prune -exec mkdir -p ./another/dir/{} \; -exec mv -i {} ./NODE_MODULES/{} \;

This will keep the directory structure.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FatihAziz
  • 438
  • 4
  • 11
3

Note: This is just improving on the accepted answer, please use the accepted answer first.

If you are so bored then keep reading.

Basically, this command should work fine for 99% of cases

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

I notice that deleting files via the command line is longer than deleting a folder via Finder (when deleting from Finder it moves that folder to ~/.Trash directory).

So if you want to move node_modules to ~/.Trash folder then you can try

find . -name 'node_modules' -type d -prune -exec sh -c 'mv -f "$1" "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" && mv "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" ~/.Trash/' sh {} \;

as you notice it consist of 2 parts.

  1. find . -name 'node_modules' -type d -prune find all node_module dirs
  2. -exec sh -c 'mv -f "$1" "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" && mv "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" ~/.Trash/' sh {} \; rename node_module by prefixing it with it's parent folder name and move it to Trash

Before I had

~/Development/angular-projects
 ┣ project1
 ┣ project2
 ┗ project3

After running command

~/.Trash
 ┣ ~project1_node_modules
 ┣ ~project2_node_modules
 ┗ ~project3_node_modules

Then make sure to empty trash enter image description here

Or Turn On empty trash feature enter image description here

sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
2

A simple trick to remove all node_modules folders in your servers (which can reduce a lot of space) is to run:

# For Ubuntu
sudo find / -not -path "/usr/lib/*" -name 'node_modules' -type d -prune -exec rm -rf '{}' +

# For macOS
sudo find / -not -path "/usr/local/*" -name 'node_modules' -type d -prune -exec rm -rf '{}' +

Here we need to exclude /usr/lib/* because if you won’t, it will delete your npm and you need to reinstall it :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chau Giang
  • 1,414
  • 11
  • 19
2

Quite late to the party, but doesn't this remove all node_modules recursively (on Linux and MacOS at least)?

rm -rf node_modules */node_modules */*/node_modules
zaplec
  • 1,681
  • 4
  • 23
  • 51
0

Python Script to Delete the node_modules folder from multiple projects. Just place it in your project folder consisting multiple projects and run it.

import os
import shutil
dirname = '/root/Desktop/proj' #Your Full Path of Projects Folder
dirfiles = os.listdir(dirname)

fullpaths = map(lambda name: os.path.join(dirname, name), dirfiles)

dirs = []

for file in fullpaths:
    if os.path.isdir(file): dirs.append(file)

for i in dirs:
    dirfiles1 = os.listdir(i)
    fullpaths1 = map(lambda name: os.path.join(i, name), dirfiles1)
    dirs1 = []
    for file in fullpaths1:
        if os.path.isdir(file):
            dirs1.append(file)
            if(file[-12:]=='node_modules'):
                shutil.rmtree(file)
                print(file)
    
aniket dhole
  • 91
  • 1
  • 7
  • 3
    seems a bit overkill for something that can be done in one line from basic linux programs and probably even windows – Emobe May 14 '21 at 18:29