BASH

Clear Common Web Project Cache Directories

A Bash script to quickly clear common cache and temporary directories for various web development projects (e.g., Laravel, Node.js builds) to resolve stale data issues.

#!/bin/bash

# Define common cache directories relative to the project root
CACHE_DIRS=(
  "storage/framework/cache"
  "storage/framework/sessions"
  "storage/framework/views"
  "bootstrap/cache" # Laravel
  "node_modules/.cache" # Node.js/Webpack
  "web/cache" # Symfony, Drupal
  "tmp"
  "var/cache" # Symfony
)

PROJECT_ROOT=$(pwd)

echo "Attempting to clear cache directories in $PROJECT_ROOT"

for dir in "${CACHE_DIRS[@]}"; do
  FULL_PATH="$PROJECT_ROOT/$dir"
  if [ -d "$FULL_PATH" ]; then
    echo "Clearing $FULL_PATH/*..."
    rm -rf "$FULL_PATH/*"
    if [ $? -eq 0 ]; then
      echo "Successfully cleared $dir"
    else
      echo "Failed to clear $dir"
    fi
  else
    echo "Directory $dir not found, skipping."
  fi
done

echo "Cache clearing attempt complete."
How it works: This script iterates through a predefined list of common cache and temporary directories found in many web projects (e.g., Laravel's framework caches, Node.js `node_modules/.cache`, Symfony/Drupal cache directories). For each directory that exists, it uses `rm -rf` to remove its contents, effectively clearing the cache. This is a common operation for web developers to resolve issues related to stale data, old compiled views, or build artifacts.

Need help integrating this into your project?

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

Hire DigitalCodeLabs