Misc notes
- I read about the Frank-Wolfe optimization algorithm. I wonder if it’s easy to implement.
Delete old Cloudflare Pages deployments
With every deploy from the Wrangler CLI, I am ending up with an ever-growing pile of CloudFlare deployments. This was getting out of hand quickly, especially with quick write-deploy-test loops that I liked for fast feedback.
I wanted to delete the old deployments that were no longer needed, but the Cloudflare UI is not very good at doing this bulk operation.
To perform this “delete automation”, I ended up writing a small script in Python, using the Cloudflare API. The relevant APIs were
- Pages Project > Get projects
- Pages Deployment > Get deployments
- Pages Deployment > Delete deployment
Here’s the code I ended up writing
import requests
auth_key = "66264d58db29a0f05b9e647bdf36be3c"
auth_email = "leo@example.com"
account_id = "6791375f8ebd84997b966bc0ebc51c10"
headers = {
"X-Auth-Key": auth_key,
"X-Auth-Email": auth_email,
}
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/pages/projects"
name_id = {}
r = requests.get(url, headers=headers)
for proj in r.json()["result"]:
name = proj["name"]
_id = proj["canonical_deployment"]["id"]
name_id[name] = _id
for n in name_id:
print("-", n)
while True:
target_project = input("Pick a name: ")
if target_project in name_id:
break
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/pages/projects/{target_project}/deployments"
r = requests.get(url, headers=headers)
for depl in r.json()["result"]:
_id = depl["id"]
if _id == name_id[target_project]:
continue
# Remove deployment
print(f"Deleting {_id}...")
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/pages/projects/{target_project}/deployments/{_id}"
requests.delete(url, headers=headers)
Handling pagination is left as an exercise to the reader. Me? I just execute the script multiple times.