Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions deploy/cloudrun/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,16 +59,21 @@ LB_NAME="${LB_NAME:-lightspeed-lb}"

# Parse arguments
FORCE=false
PURGE_DATA=false

while [[ $# -gt 0 ]]; do
case $1 in
--force)
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
Expand All @@ -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
Expand Down Expand Up @@ -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
# =============================================================================
Expand All @@ -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"
Expand Down
Loading