24

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?

1
  • 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. Commented Sep 25, 2022 at 1:39

1 Answer 1

46

To find and delete all node_modules recursively:

find . -name "node_modules" -prune -exec rm -rf '{}' + 
Sign up to request clarification or add additional context in comments.

4 Comments

What ending sign "+" do in expression above? I don't see this syntax before. Anyway thank you, command works nice.
@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: stackoverflow.com/a/6085237
Could add -type d
Mention that this command is to delete node_modules, in BOLD LETTERS

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.