BASH

Clean Up Web Project Build Artifacts and Dependencies

Automate the removal of common web project artifacts like `node_modules`, `dist` folders, and cache directories to ensure clean builds and save disk space.

#!/bin/bash

echo "Starting web project cleanup..."

# Array of directories/files to clean
CLEANUP_PATHS=(
  "node_modules"
  "dist"
  "build"
  ".parcel-cache"
  ".next"
  "coverage"
  "tmp"
  "npm-debug.log"
  "yarn-error.log"
)

for PATH_ITEM in "${CLEANUP_PATHS[@]}"; do
  if [ -e "$PATH_ITEM" ]; then # Checks if the item exists (file or directory)
    echo "Removing $PATH_ITEM..."
    rm -rf "$PATH_ITEM"
    if [ $? -eq 0 ]; then
      echo "Successfully removed $PATH_ITEM"
    else
      echo "Error removing $PATH_ITEM"
    fi
  else
    echo "Skipping $PATH_ITEM (not found)."
  fi
done

echo "Cleanup complete."
How it works: This script iterates through a predefined list of common web project directories and files (like `node_modules`, `dist`, cache folders) and removes them. This is extremely useful for web developers to ensure clean builds, free up disk space, and resolve potential caching issues, making it a staple in development workflows and CI/CD pipelines before fresh installations or builds.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs