diff --git a/deploy/cloudrun/cleanup.sh b/deploy/cloudrun/cleanup.sh index c4551943..8632055c 100755 --- a/deploy/cloudrun/cleanup.sh +++ b/deploy/cloudrun/cleanup.sh @@ -10,10 +10,11 @@ # - Service accounts (runtime + Pub/Sub invoker) and IAM bindings # # Usage: -# ./deploy/cloudrun/cleanup.sh [--force] +# ./deploy/cloudrun/cleanup.sh [--force] [--purge-data] # # Options: -# --force Skip confirmation prompt +# --force Skip confirmation prompt +# --purge-data Also delete CloudSQL instances (lists Redis for manual cleanup) # # Prerequisites: # - gcloud CLI installed and authenticated @@ -58,6 +59,7 @@ LB_NAME="${LB_NAME:-lightspeed-lb}" # Parse arguments FORCE=false +PURGE_DATA=false while [[ $# -gt 0 ]]; do case $1 in @@ -65,9 +67,13 @@ while [[ $# -gt 0 ]]; do FORCE=true shift ;; + --purge-data) + PURGE_DATA=true + shift + ;; *) log_error "Unknown option: $1" - echo "Usage: $0 [--force]" + echo "Usage: $0 [--force] [--purge-data]" exit 1 ;; esac @@ -92,6 +98,12 @@ echo " session-database-url, gma-client-id, gma-client-secret, dcr-e echo " rate-limit-redis-url" echo " - Service accounts: $SERVICE_ACCOUNT" echo " $PUBSUB_INVOKER_SA" +if [ "$PURGE_DATA" = true ]; then + echo "" + log_warn "DATA PURGE ENABLED — the following will also be PERMANENTLY deleted:" + echo " - CloudSQL instances matching 'lightspeed' (IRREVERSIBLE DATA LOSS)" + echo " - Redis instances will be listed for manual cleanup" +fi echo "" # Confirmation prompt @@ -292,6 +304,26 @@ else log_info "Service account '$PUBSUB_INVOKER_SA' does not exist, skipping" fi +# ============================================================================= +# Step 5: Purge Data Resources (optional) +# ============================================================================= +if [ "$PURGE_DATA" = true ]; then + echo "" + log_info "Purging data resources..." + # Delete CloudSQL instances + for instance in $(gcloud sql instances list --project="$PROJECT_ID" --filter="name~^lightspeed" --format="value(name)" 2>/dev/null); do + echo "Deleting CloudSQL instance: $instance" + if ! gcloud sql instances delete "$instance" --project="$PROJECT_ID" --quiet; then + log_warn "Failed to delete CloudSQL instance: $instance" + fi + done + # List Redis instances for manual cleanup + for instance in $(gcloud redis instances list --region="$REGION" --project="$PROJECT_ID" --format="value(name)" 2>/dev/null); do + echo "Note: Redis instance $instance must be deleted manually or via console" + done + log_info "Data purge complete." +fi + # ============================================================================= # Summary # ============================================================================= @@ -306,10 +338,16 @@ echo " - Load balancer resources (if any existed)" echo " - Pub/Sub topic and subscription" echo " - Secret Manager secrets" echo " - Service accounts (runtime + Pub/Sub invoker) and IAM bindings" +if [ "$PURGE_DATA" = true ]; then + echo " - CloudSQL instances matching 'lightspeed'" +fi echo "" + echo "Note: The following resources were NOT deleted (delete manually if needed):" -echo " - Cloud SQL instances" -echo " - Cloud Memorystore Redis instances" +if [ "$PURGE_DATA" != true ]; then + echo " - Cloud SQL instances (use --purge-data to delete)" + echo " - Cloud Memorystore Redis instances (use --purge-data to delete)" +fi echo " - Container images in GCR/Artifact Registry" echo " - VPC connectors" echo " - Cloud Build triggers"