← Kembali

Bash Pemula Utility

Cleanup node_modules + .next dari semua project

Hapus folder node_modules dan build artifact dari semua project di /Documents. Hemat 30-100 GB disk dalam satu menit.

Dipublikasikan 19 Mei 2026

Punya 50+ project Node.js di laptop = 50+ folder node_modules yang totalnya 30-100 GB. Setiap project pulang dari liburan, sebelum re-install yang baru, run snippet ini untuk reset. Bonus: hapus juga .next, dist, .cache dari project yang tidak dipakai lebih dari 30 hari.

Kode (preview dulu)

#!/usr/bin/env bash
# cleanup-node.sh — Hapus node_modules + build artifact dari project lama.
# Selalu run dengan --dry-run dulu untuk preview.

set -euo pipefail

ROOT="${1:-$HOME/Documents}"
DRY_RUN="${2:-true}"

if [ ! -d "$ROOT" ]; then
  echo "Error: $ROOT tidak ada" >&2
  exit 1
fi

echo "Scanning $ROOT ..."
echo ""

# Cari semua node_modules, hitung total size
TOTAL_SIZE=0
COUNT=0

while IFS= read -r -d '' dir; do
  SIZE=$(du -sk "$dir" 2>/dev/null | awk '{print $1}')
  SIZE=${SIZE:-0}
  TOTAL_SIZE=$((TOTAL_SIZE + SIZE))
  COUNT=$((COUNT + 1))
  HUMAN=$(du -sh "$dir" 2>/dev/null | awk '{print $1}')
  echo "  $HUMAN  $dir"
done < <(find "$ROOT" -name node_modules -type d -prune -print0 2>/dev/null)

echo ""
echo "Total: $COUNT folder, $((TOTAL_SIZE / 1024)) MB"
echo ""

if [ "$DRY_RUN" = "true" ]; then
  echo "Ini DRY RUN — tidak ada yang dihapus."
  echo "Untuk benar-benar hapus, run: $0 $ROOT false"
  exit 0
fi

# Konfirmasi sebelum hapus
read -p "Hapus semua $COUNT folder ini? (yes/no) " ANSWER
if [ "$ANSWER" != "yes" ]; then
  echo "Dibatalkan."
  exit 0
fi

# Hapus
find "$ROOT" -name node_modules -type d -prune -exec rm -rf {} +
find "$ROOT" -name .next -type d -prune -exec rm -rf {} +
find "$ROOT" -name dist -type d -prune -exec rm -rf {} +
find "$ROOT" -name .turbo -type d -prune -exec rm -rf {} +

echo "Selesai. Hemat $((TOTAL_SIZE / 1024)) MB."

Pemakaian

# Preview dulu (DRY RUN, default)
./cleanup-node.sh ~/Documents

# Output:
#   Scanning /Users/me/Documents ...
#     289M  /Users/me/Documents/project-a/node_modules
#     412M  /Users/me/Documents/project-b/node_modules
#     ...
#   Total: 24 folder, 8542 MB
#   Ini DRY RUN — tidak ada yang dihapus.

# Kalau hasil preview-nya OK, run hapus
./cleanup-node.sh ~/Documents false

Variasi: hapus kalau project tidak dimodifikasi > 30 hari

Filter project yang sudah lama tidak disentuh — saver yang lebih aman:

# Hanya hapus node_modules dari project yang folder root-nya tidak modified 30+ hari
find "$ROOT" -name node_modules -type d -prune \
  | while IFS= read -r dir; do
      parent=$(dirname "$dir")
      if [ -z "$(find "$parent" -maxdepth 2 -type f -mtime -30 -not -path '*/node_modules/*' 2>/dev/null | head -1)" ]; then
        echo "$dir"  # candidate untuk dihapus
      fi
    done

Kapan dipakai

  • Sebelum laptop hampir penuh dan macOS mulai nge-warn.
  • Setelah finalize project, tidak akan disentuh lagi minggu ini.
  • Sebelum sync ke iCloud / OneDrive / Dropbox — node_modules jangan masuk cloud sync, akan crash sync engine.

Catatan

  • -prune prevent find masuk ke dalam node_modules. Tanpa ini, find akan recurse ke node_modules/some-pkg/node_modules (nested) dan jadi exponential slow di project monorepo lama.
  • rm -rf tidak reversible. Always run dengan dry-run dulu.
  • pnpm global store (~/.local/share/pnpm/store) tidak terkena script ini — itu cache shared, tidak akan duplikat. Bisa jadi 5-10 GB sendiri.
  • Setelah cleanup, project butuh bun install / pnpm install lagi sebelum bisa di-run. Tidak masalah.

Tambahkan ke crontab mingguan kalau confident: 0 3 * * 0 ~/scripts/cleanup-node.sh ~/Documents false >> ~/cleanup.log 2>&1. Tapi pertimbangkan: ada hari Senin kamu butuh project lama di-resume.

# tags

disk-cleanupnode_modulesfind

← Semua snippet Snippet Bash lain →