|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | 3 | # Cleanup script for test workers |
4 | | -# This script attempts to delete workers with test-related names |
| 4 | +# Lists all workers and deletes those matching test patterns |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Check required environment variables |
| 9 | +if [ -z "$CLOUDFLARE_ACCOUNT_ID" ]; then |
| 10 | + echo "❌ Error: CLOUDFLARE_ACCOUNT_ID environment variable is required" |
| 11 | + echo " Set it with: export CLOUDFLARE_ACCOUNT_ID='your-account-id'" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +if [ -z "$CLOUDFLARE_API_TOKEN" ]; then |
| 16 | + echo "❌ Error: CLOUDFLARE_API_TOKEN environment variable is required" |
| 17 | + echo " Set it with: export CLOUDFLARE_API_TOKEN='your-api-token'" |
| 18 | + exit 1 |
| 19 | +fi |
5 | 20 |
|
6 | | -ACCOUNT_ID="1634a8e653b2ce7e0f7a23cca8cbd86a" |
7 | 21 | DELETED_COUNT=0 |
8 | 22 | FAILED_COUNT=0 |
9 | 23 |
|
10 | 24 | echo "🧹 Cleaning up test workers..." |
11 | | -echo "Account ID: $ACCOUNT_ID" |
| 25 | +echo "Account ID: $CLOUDFLARE_ACCOUNT_ID" |
12 | 26 | echo "" |
13 | 27 |
|
14 | | -# Common test worker name patterns based on the error message |
15 | | -# Example: test-project-smoke-test-defeated-cat-c6cefbc2 |
| 28 | +# Test patterns to identify test workers |
16 | 29 | test_patterns=( |
17 | | - "test-project-smoke-test-" |
18 | | - "smoke-test-" |
19 | | - "e2e-test-" |
20 | | - "playground-test-" |
21 | | - "hello-world-" |
22 | | - "minimal-" |
23 | | - "standard-" |
| 30 | + "smoke-test" |
| 31 | + "e2e-test" |
| 32 | + "test-project" |
| 33 | + "playground" |
| 34 | + "hello-world" |
| 35 | + "minimal" |
| 36 | + "standard" |
24 | 37 | ) |
25 | 38 |
|
26 | | -# Common animal names and random suffixes used in test worker names |
27 | | -animals=( |
28 | | - "defeated-cat" "happy-dog" "clever-fox" "swift-bird" "brave-lion" "wise-owl" |
29 | | - "quick-rabbit" "strong-bear" "gentle-deer" "proud-eagle" "calm-turtle" |
30 | | - "bright-fish" "wild-wolf" "kind-sheep" "fast-horse" "small-mouse" |
31 | | - "tall-giraffe" "big-elephant" "cute-panda" "red-fox" "blue-whale" |
32 | | -) |
| 39 | +echo "📋 Fetching list of all workers..." |
33 | 40 |
|
34 | | -# Common hex suffixes (8 characters) |
35 | | -hex_suffixes=( |
36 | | - "c6cefbc2" "a1b2c3d4" "e5f6g7h8" "i9j0k1l2" "m3n4o5p6" "q7r8s9t0" |
37 | | - "1a2b3c4d" "5e6f7g8h" "9i0j1k2l" "3m4n5o6p" "7q8r9s0t" "1u2v3w4x" |
38 | | - "5y6z7a8b" "9c0d1e2f" "3g4h5i6j" "7k8l9m0n" "1o2p3q4r" "5s6t7u8v" |
39 | | - "9w0x1y2z" "3a4b5c6d" "7e8f9g0h" "1i2j3k4l" "5m6n7o8p" "9q0r1s2t" |
40 | | -) |
| 41 | +# Get list of all workers |
| 42 | +workers_response=$(curl -s -X GET \ |
| 43 | + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ |
| 44 | + "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts") |
| 45 | + |
| 46 | +# Check if API call was successful |
| 47 | +if ! echo "$workers_response" | jq -e '.success' >/dev/null 2>&1; then |
| 48 | + echo "❌ Failed to fetch workers list:" |
| 49 | + echo "$workers_response" | jq -r '.errors[]?.message // "Unknown error"' |
| 50 | + exit 1 |
| 51 | +fi |
41 | 52 |
|
42 | | -echo "Attempting to delete workers with test patterns..." |
43 | | -echo "This may take a few minutes..." |
| 53 | +# Extract worker names |
| 54 | +worker_names=$(echo "$workers_response" | jq -r '.result[]?.id // empty') |
| 55 | + |
| 56 | +if [ -z "$worker_names" ]; then |
| 57 | + echo "ℹ️ No workers found in account" |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +total_workers=$(echo "$worker_names" | wc -l | tr -d ' ') |
| 62 | +echo "📊 Found $total_workers total workers" |
44 | 63 | echo "" |
45 | 64 |
|
46 | | -# Try to delete workers with various patterns and suffixes |
47 | | -for pattern in "${test_patterns[@]}"; do |
48 | | - echo "🔍 Trying pattern: $pattern*" |
49 | | - |
50 | | - for animal in "${animals[@]}"; do |
51 | | - for hex in "${hex_suffixes[@]}"; do |
52 | | - worker_name="${pattern}${animal}-${hex}" |
53 | | - |
54 | | - # Try to delete the worker |
55 | | - if CLOUDFLARE_ACCOUNT_ID="$ACCOUNT_ID" npx wrangler delete --name "$worker_name" --force >/dev/null 2>&1; then |
56 | | - echo " ✅ Deleted: $worker_name" |
57 | | - DELETED_COUNT=$((DELETED_COUNT + 1)) |
58 | | - else |
59 | | - FAILED_COUNT=$((FAILED_COUNT + 1)) |
60 | | - fi |
61 | | - |
62 | | - # Show progress every 50 attempts |
63 | | - if [ $((($DELETED_COUNT + $FAILED_COUNT) % 50)) -eq 0 ]; then |
64 | | - echo " 📊 Progress: $DELETED_COUNT deleted, $FAILED_COUNT not found" |
65 | | - fi |
66 | | - done |
| 65 | +echo "🔍 Identifying test workers to delete..." |
| 66 | + |
| 67 | +# Find workers matching test patterns |
| 68 | +test_workers=() |
| 69 | +while IFS= read -r worker_name; do |
| 70 | + for pattern in "${test_patterns[@]}"; do |
| 71 | + if [[ "$worker_name" == *"$pattern"* ]]; then |
| 72 | + test_workers+=("$worker_name") |
| 73 | + echo " 🎯 Found test worker: $worker_name" |
| 74 | + break |
| 75 | + fi |
67 | 76 | done |
| 77 | +done <<< "$worker_names" |
| 78 | + |
| 79 | +if [ ${#test_workers[@]} -eq 0 ]; then |
| 80 | + echo "ℹ️ No test workers found matching patterns: ${test_patterns[*]}" |
| 81 | + exit 0 |
| 82 | +fi |
| 83 | + |
| 84 | +echo "" |
| 85 | +echo "🗑️ Deleting ${#test_workers[@]} test workers..." |
| 86 | + |
| 87 | +# Delete each test worker |
| 88 | +for worker_name in "${test_workers[@]}"; do |
| 89 | + echo " Deleting: $worker_name" |
| 90 | + |
| 91 | + delete_response=$(curl -s -X DELETE \ |
| 92 | + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ |
| 93 | + "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$worker_name") |
| 94 | + |
| 95 | + if echo "$delete_response" | jq -e '.success' >/dev/null 2>&1; then |
| 96 | + echo " ✅ Deleted successfully" |
| 97 | + DELETED_COUNT=$((DELETED_COUNT + 1)) |
| 98 | + else |
| 99 | + echo " ❌ Failed to delete:" |
| 100 | + echo "$delete_response" | jq -r '.errors[]?.message // "Unknown error"' | sed 's/^/ /' |
| 101 | + FAILED_COUNT=$((FAILED_COUNT + 1)) |
| 102 | + fi |
68 | 103 | done |
69 | 104 |
|
70 | 105 | echo "" |
71 | 106 | echo "🎯 Cleanup Summary:" |
72 | 107 | echo " ✅ Workers deleted: $DELETED_COUNT" |
73 | | -echo " ❌ Workers not found: $FAILED_COUNT" |
| 108 | +echo " ❌ Failed deletions: $FAILED_COUNT" |
74 | 109 | echo "" |
75 | 110 |
|
76 | 111 | if [ $DELETED_COUNT -gt 0 ]; then |
77 | 112 | echo "✨ Successfully cleaned up $DELETED_COUNT test workers!" |
| 113 | + echo " This should help resolve the 500 worker limit issue." |
78 | 114 | else |
79 | | - echo "ℹ️ No test workers found with the attempted patterns." |
80 | | - echo " You may need to manually delete workers via the Cloudflare dashboard:" |
81 | | - echo " https://dash.cloudflare.com/1634a8e653b2ce7e0f7a23cca8cbd86a/workers-and-pages" |
| 115 | + echo "⚠️ No test workers were deleted." |
| 116 | + echo " You may need to manually review workers in the dashboard:" |
| 117 | + echo " https://dash.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/workers-and-pages" |
82 | 118 | fi |
0 commit comments