2023 08 03


Reading time: about 1 minute

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

  1. Pages Project > Get projects
  2. Pages Deployment > Get deployments
  3. 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.

Citation

If you find this work useful, please cite it as:
@article{yaltirakli,
  title   = "2023 08 03",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/journal/2023-08-03"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "2023 08 03", December, 2024. [Online]. Available: https://www.gkbrk.com/journal/2023-08-03. [Accessed Dec. 17, 2024].
APA Style
Yaltirakli, G. (2024, December 17). 2023 08 03. https://www.gkbrk.com/journal/2023-08-03
Bluebook Style
Gokberk Yaltirakli, 2023 08 03, GKBRK.COM (Dec. 17, 2024), https://www.gkbrk.com/journal/2023-08-03

Comments

© 2024 Gokberk Yaltirakli