Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove all node_module folders recursively. Useful to free up disk space by removing the node_module from those projects you have not used for a while.
# Simple version using only "find"
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
# More helpful version with some log messages and with option of dry mode
# Usage:
# MODE: DRY
# sh remove-node_module-recursively-2.sh ~/my-path-with-many-projects
# MODE: REMOVE
# sh remove-node_module-recursively-2.sh ~/my-path-with-many-projects remove
REMOVE_PATH=$1
MODE=$2
echo "Removing all node_modules from $REMOVE_PATH"
if [ "$MODE" != "remove" ]; then
echo "-- DRY MODE --"
fi
for line in $(find $REMOVE_PATH -name "node_modules" -type d -prune); do
found=`echo "$line" | grep -o node_modules | wc -l`
if [ $found -eq 1 ]; then
echo "-> $line"
if [ "$MODE" = "remove" ]; then
echo "Removing..."
rm -rf "$line"
else
echo "Skip (dry mode enabled)"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment