Sumit
Sumit

Reputation: 4252

Delete node_modules folder recursively from a specified path using command line

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?

Upvotes: 371

Views: 150993

Answers (14)

David Alsh
David Alsh

Reputation: 7631

I wrote a tool in Rust for this with support for Windows, MacOS and Linux called rrm - GitHub

cd $HOME/Development
rrm -f node_modules .

Outputs

Looking within:
  /home/alshdavid/Development

Looking for:
  node_modules

Found:
  /home/alshdavid/Development/my-project-a/node_modules
  /home/alshdavid/Development/my-project-b/node_modules
  /home/alshdavid/Development/my-project-c/node_modules

Delete matches? [y/N]

I wrote this because I wanted to use the same command line syntax on all platforms because I use Windows, MacOS and Linux.

It's a 200kb binary that does the trick

Upvotes: 0

jeckep
jeckep

Reputation: 3938

Try npkill tool.

npx npkill

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

npkill

Upvotes: 329

Darman
Darman

Reputation: 31

To expand on the previous answers: Here is a script that automatically deletes all */node_modules and */dist folders to clean out any installed libraries.

#!/bin/bash
# Script to clean all:
#   - */node_modules
#   - */dist

scriptDir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "$scriptDir"

echo "Removing node_modules..."
args=(
    . \( -name 'dist' -o -name 'node_modules' \)    # find all folders with name dist or node_modules
    -prune                                          # exclude recursive search in subdirectories
    -print                                          # output results
    -exec rm -rf '{}' +                             # delete all folders
)
find "${args[@]}"
echo "Done."

Upvotes: 0

zaplec
zaplec

Reputation: 1819

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

Upvotes: 3

sultanmyrza
sultanmyrza

Reputation: 5392

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

Upvotes: 6

noseratio
noseratio

Reputation: 61666

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

Upvotes: 16

Simon Tran
Simon Tran

Reputation: 2070

In Bash, you can simply use

rm -rf node_modules **/node_modules

Upvotes: 13

Chau Giang
Chau Giang

Reputation: 1554

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

Upvotes: 2

Sidharth
Sidharth

Reputation: 1925

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

Upvotes: 122

Darius
Darius

Reputation: 12052

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 {} +

Upvotes: 837

FatihAziz
FatihAziz

Reputation: 453

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.

Upvotes: 3

aniket dhole
aniket dhole

Reputation: 91

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)
    

Upvotes: 0

Ivan V.
Ivan V.

Reputation: 8081

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 {} \; 

}

Upvotes: 7

Sumit
Sumit

Reputation: 4252

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 '{}' +

Upvotes: 13

Related Questions