BASH

Unified Script for Clearing Application Caches (e.g., Laravel, Node.js)

Streamline your development workflow with a flexible bash script to clear various application caches (Laravel, Node.js, Composer) based on project type.

#!/bin/bash

# Define paths relative to the script's execution directory
# Or, set specific absolute paths if preferred
WEBAPP_ROOT="."

# --- Cache Clearing Functions ---
clear_laravel_cache() {
  echo "Clearing Laravel cache..."
  if [ -f "$WEBAPP_ROOT/artisan" ]; then
    (cd "$WEBAPP_ROOT" && php artisan cache:clear)
    (cd "$WEBAPP_ROOT" && php artisan config:clear)
    (cd "$WEBAPP_ROOT" && php artisan view:clear)
    echo "Laravel cache cleared."
  else
    echo "Laravel artisan command not found at $WEBAPP_ROOT/artisan."
  fi
}

clear_node_cache() {
  echo "Clearing Node.js/NPM cache..."
  # npm cache clean --force is deprecated in newer npm versions
  # Consider 'npm cache verify' if just verifying integrity, or manually deleting node_modules
  # For a true 'clean', deleting node_modules and re-installing is common.
  if command -v npm &> /dev/null; then
    echo "Attempting 'npm cache clean --force' (might be deprecated)..."
    npm cache clean --force # Use with caution or consider alternatives
    # Optionally: rm -rf "$WEBAPP_ROOT/node_modules" && (cd "$WEBAPP_ROOT" && npm install)
    echo "Node.js/NPM cache operations completed (or attempted)."
  else
    echo "NPM command not found. Skipping Node.js cache."
  fi
}

clear_composer_cache() {
  echo "Clearing Composer cache..."
  if command -v composer &> /dev/null; then
    composer clear-cache
    echo "Composer cache cleared."
  else
    echo "Composer command not found. Skipping Composer cache."
  fi
}

# --- Main Logic ---
if [ -z "$1" ]; then
  echo "Usage: $0 [all|laravel|node|composer]"
  exit 1
fi

case "$1" in
  all)
    clear_laravel_cache
    clear_node_cache
    clear_composer_cache
    ;;
  laravel)
    clear_laravel_cache
    ;;
  node)
    clear_node_cache
    ;;
  composer)
    clear_composer_cache
    ;;
  *)
    echo "Invalid option: $1"
    echo "Usage: $0 [all|laravel|node|composer]"
    exit 1
    ;;
esac

echo "Cache clearing script finished."
How it works: This bash script provides a unified way to clear various application caches, which is a common task for web developers. It includes functions for clearing Laravel, Node.js (NPM), and Composer caches. The script can be run with arguments like `all`, `laravel`, `node`, or `composer` to specify which caches to clear. It checks for the presence of relevant commands (`artisan`, `npm`, `composer`) before attempting to clear the caches. This helps streamline development workflows by centralizing cache management.

Need help integrating this into your project?

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

Hire DigitalCodeLabs