Skip to content

Commit 52733d0

Browse files
committed
Refactor Cloudflare test worker cleanup scripts to use API instead of brute-force guessing.
1 parent 44baabf commit 52733d0

File tree

3 files changed

+200
-114
lines changed

3 files changed

+200
-114
lines changed

.notes/justin/worklogs/2025-09-20-ci-improvements.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,22 @@ Need to clean up test workers with "smoke-test" in their names using Wrangler CL
6262
- Ran cleanup scripts but found no workers matching the attempted patterns
6363
- Workers may have been auto-cleaned, use different patterns, or need manual dashboard cleanup
6464

65-
### Cleanup scripts created
66-
- `scripts/cleanup-test-workers.sh`: Comprehensive cleanup with many pattern variations
67-
- `scripts/quick-cleanup.sh`: Focused cleanup with most likely patterns
68-
69-
### Manual cleanup guidance
70-
Dashboard URL: https://dash.cloudflare.com/1634a8e653b2ce7e0f7a23cca8cbd86a/workers-and-pages
71-
Look for workers containing: smoke-test, e2e-test, test-project, playground, hello-world, minimal, standard
65+
### Cleanup scripts created (revised to use Cloudflare API)
66+
- `scripts/cleanup-test-workers.sh`: Lists all workers via API, deletes those matching test patterns
67+
- `scripts/quick-cleanup.sh`: Dry-run mode by default, shows what would be deleted
68+
69+
### API approach
70+
- Uses `curl` with Cloudflare API instead of brute-force guessing
71+
- Lists workers: `GET /accounts/$ACCOUNT_ID/workers/scripts`
72+
- Deletes workers: `DELETE /accounts/$ACCOUNT_ID/workers/scripts/<script-name>`
73+
- Requires environment variables: `CLOUDFLARE_ACCOUNT_ID` and `CF_API_TOKEN`
74+
- No hardcoded account IDs
75+
76+
### Usage
77+
```bash
78+
export CLOUDFLARE_ACCOUNT_ID='1634a8e653b2ce7e0f7a23cca8cbd86a'
79+
export CF_API_TOKEN='your-token'
80+
./scripts/quick-cleanup.sh # dry run
81+
./scripts/quick-cleanup.sh --delete # actual deletion
82+
./scripts/cleanup-test-workers.sh # comprehensive cleanup
83+
```

scripts/cleanup-test-workers.sh

Lines changed: 89 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,118 @@
11
#!/bin/bash
22

33
# 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
520

6-
ACCOUNT_ID="1634a8e653b2ce7e0f7a23cca8cbd86a"
721
DELETED_COUNT=0
822
FAILED_COUNT=0
923

1024
echo "🧹 Cleaning up test workers..."
11-
echo "Account ID: $ACCOUNT_ID"
25+
echo "Account ID: $CLOUDFLARE_ACCOUNT_ID"
1226
echo ""
1327

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
1629
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"
2437
)
2538

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..."
3340

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
4152

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"
4463
echo ""
4564

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
6776
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
68103
done
69104

70105
echo ""
71106
echo "🎯 Cleanup Summary:"
72107
echo " ✅ Workers deleted: $DELETED_COUNT"
73-
echo "Workers not found: $FAILED_COUNT"
108+
echo "Failed deletions: $FAILED_COUNT"
74109
echo ""
75110

76111
if [ $DELETED_COUNT -gt 0 ]; then
77112
echo "✨ Successfully cleaned up $DELETED_COUNT test workers!"
113+
echo " This should help resolve the 500 worker limit issue."
78114
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"
82118
fi

scripts/quick-cleanup.sh

Lines changed: 92 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,117 @@
11
#!/bin/bash
22

33
# Quick cleanup script for test workers
4-
# Focuses on the most likely patterns based on the error message
4+
# Lists workers and shows which ones would be deleted (dry-run mode by default)
55

6-
ACCOUNT_ID="1634a8e653b2ce7e0f7a23cca8cbd86a"
7-
DELETED_COUNT=0
6+
set -e
87

9-
echo "🧹 Quick cleanup of test workers..."
10-
echo ""
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
1114

12-
# Try the exact worker from the error message first
13-
echo "Trying exact worker from error message..."
14-
if CLOUDFLARE_ACCOUNT_ID="$ACCOUNT_ID" npx wrangler delete --name "test-project-smoke-test-defeated-cat-c6cefbc2" --force >/dev/null 2>&1; then
15-
echo " ✅ Deleted: test-project-smoke-test-defeated-cat-c6cefbc2"
16-
DELETED_COUNT=$((DELETED_COUNT + 1))
15+
if [ -z "$CLOUDFLARE_API_TOKEN" ]; then
16+
echo "❌ Error: CF_API_TOKEN environment variable is required"
17+
echo " Set it with: export CF_API_TOKEN='your-api-token'"
18+
exit 1
19+
fi
20+
21+
# Check if this is a dry run (default) or actual deletion
22+
DRY_RUN=true
23+
if [ "$1" = "--delete" ]; then
24+
DRY_RUN=false
25+
echo "🚨 DELETION MODE: Will actually delete test workers"
1726
else
18-
echo " ❌ Not found: test-project-smoke-test-defeated-cat-c6cefbc2"
27+
echo "🔍 DRY RUN MODE: Will only show what would be deleted"
28+
echo " Use --delete flag to actually delete workers"
1929
fi
2030

21-
# Try some variations of the pattern
22-
patterns=(
23-
"test-project-smoke-test-"
24-
"hello-world-smoke-test-"
25-
"minimal-smoke-test-"
26-
"standard-smoke-test-"
27-
"playground-e2e-test-"
28-
)
31+
echo "Account ID: $CLOUDFLARE_ACCOUNT_ID"
32+
echo ""
2933

30-
# Some realistic animal-hex combinations
31-
realistic_suffixes=(
32-
"defeated-cat-c6cefbc2"
33-
"happy-dog-a1b2c3d4"
34-
"clever-fox-e5f6g7h8"
35-
"swift-bird-i9j0k1l2"
36-
"brave-lion-m3n4o5p6"
34+
# Test patterns to identify test workers
35+
test_patterns=(
36+
"smoke-test"
37+
"e2e-test"
38+
"test-project"
39+
"playground"
3740
)
3841

42+
echo "📋 Fetching list of all workers..."
43+
44+
# Get list of all workers
45+
workers_response=$(curl -s -X GET \
46+
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
47+
"https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts")
48+
49+
# Check if API call was successful
50+
if ! echo "$workers_response" | jq -e '.success' >/dev/null 2>&1; then
51+
echo "❌ Failed to fetch workers list:"
52+
echo "$workers_response" | jq -r '.errors[]?.message // "Unknown error"'
53+
exit 1
54+
fi
55+
56+
# Extract worker names
57+
worker_names=$(echo "$workers_response" | jq -r '.result[]?.id // empty')
58+
59+
if [ -z "$worker_names" ]; then
60+
echo "ℹ️ No workers found in account"
61+
exit 0
62+
fi
63+
64+
total_workers=$(echo "$worker_names" | wc -l | tr -d ' ')
65+
echo "📊 Found $total_workers total workers"
3966
echo ""
40-
echo "Trying common test worker patterns..."
41-
42-
for pattern in "${patterns[@]}"; do
43-
echo "Pattern: $pattern*"
44-
for suffix in "${realistic_suffixes[@]}"; do
45-
worker_name="${pattern}${suffix}"
46-
if CLOUDFLARE_ACCOUNT_ID="$ACCOUNT_ID" npx wrangler delete --name "$worker_name" --force >/dev/null 2>&1; then
47-
echo " ✅ Deleted: $worker_name"
48-
DELETED_COUNT=$((DELETED_COUNT + 1))
67+
68+
echo "🔍 Test workers found:"
69+
70+
# Find and optionally delete workers matching test patterns
71+
test_workers=()
72+
while IFS= read -r worker_name; do
73+
for pattern in "${test_patterns[@]}"; do
74+
if [[ "$worker_name" == *"$pattern"* ]]; then
75+
test_workers+=("$worker_name")
76+
if [ "$DRY_RUN" = true ]; then
77+
echo " 🎯 Would delete: $worker_name"
78+
else
79+
echo " 🗑️ Deleting: $worker_name"
80+
81+
delete_response=$(curl -s -X DELETE \
82+
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
83+
"https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$worker_name")
84+
85+
if echo "$delete_response" | jq -e '.success' >/dev/null 2>&1; then
86+
echo " ✅ Deleted successfully"
87+
else
88+
echo " ❌ Failed to delete"
89+
fi
90+
fi
91+
break
4992
fi
5093
done
51-
done
94+
done <<< "$worker_names"
5295

5396
echo ""
54-
echo "🎯 Quick cleanup completed: $DELETED_COUNT workers deleted"
55-
echo ""
97+
echo "📊 Summary:"
98+
echo " Total workers: $total_workers"
99+
echo " Test workers found: ${#test_workers[@]}"
56100

57-
if [ $DELETED_COUNT -eq 0 ]; then
58-
echo "ℹ️ No test workers found with common patterns."
101+
if [ ${#test_workers[@]} -eq 0 ]; then
102+
echo ""
103+
echo "ℹ️ No test workers found matching patterns: ${test_patterns[*]}"
59104
echo ""
60105
echo "📋 Manual cleanup options:"
61106
echo "1. Visit Cloudflare Dashboard:"
62-
echo " https://dash.cloudflare.com/$ACCOUNT_ID/workers-and-pages"
107+
echo " https://dash.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/workers-and-pages"
63108
echo ""
64-
echo "2. Look for workers with these patterns in their names:"
65-
echo " - smoke-test"
66-
echo " - e2e-test"
67-
echo " - test-project"
68-
echo " - playground"
69-
echo " - hello-world"
70-
echo " - minimal"
71-
echo " - standard"
109+
echo "2. Look for workers with random names that might be from tests"
110+
elif [ "$DRY_RUN" = true ]; then
72111
echo ""
73-
echo "3. Delete workers that look like temporary test deployments"
74-
echo " (usually have random animal names and hex suffixes)"
112+
echo "🚀 To actually delete these workers, run:"
113+
echo " ./scripts/quick-cleanup.sh --delete"
75114
else
76-
echo "✨ Cleaned up $DELETED_COUNT test workers!"
77-
echo " If you still hit the 500 worker limit, run this script again"
78-
echo " or manually clean up via the dashboard."
115+
echo ""
116+
echo "✨ Cleanup completed!"
79117
fi

0 commit comments

Comments
 (0)