20

I am a web developer. I checkout many OSS projects and build them. Node modules are notorious in creating too many small files. I am running out of free iNodes because of that. I want an easy way (in linux) to lookup for all node_modules and delete unwanted ones. I am using the below command to list all node_modules directory and then delete them manually.

find ~/projects -name node_modules -prune

Is there a first-class utility that provides better way to manage node_modules?

Varunkumar Nagarajan
  • 1,807
  • 1
  • 24
  • 43
  • 1
    I don't see the problem - house cleaning of big `node_modules` directories - as being solely addressable by a *find rm* approach. There's more to it: is this the root node_modules? How big is it? When's the last time it was used? After closing it with the "jeez, this question is a duplicate of find rm usage", people are **still** left with the question of how to clean up their node_modules and reclaim wasted space. Vote to reopen, SO will not explode because this one question broke the camel's back. – JL Peyret Sep 25 '22 at 01:39

1 Answers1

38

To find and delete all node_modules recursively:

find . -name "node_modules" -prune -exec rm -rf '{}' +
Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
BILL
  • 4,711
  • 10
  • 57
  • 96
  • 2
    What ending sign "+" do in expression above? I don't see this syntax before. Anyway thank you, command works nice. – Bacher Jan 09 '20 at 09:47
  • 8
    @Bacher the `+` changes how the `rm -rf` is executed. Without the `+` assuming the find output yields `./foo/node_modules` and `./bar/node_modules` two `rm -rf` commands would get run; one for each path; Including the `+` passes the list of directories to a single `rm -rf` command. In the latter case you would see `rm -rf ./foo/node_modules ./bar/node_modules`. For a more detailed explanation check out the answer here: https://stackoverflow.com/a/6085237 – Frito Jan 31 '20 at 14:59
  • 4
    Could add `-type d` – Tyler Christian Jul 02 '20 at 14:36
  • 2
    **Mention that this command is to delete `node_modules`, in BOLD LETTERS** – Nitin Kumar May 20 '22 at 07:43