While using Cloudflare Pages to deploy one of my websites, I noticed a long list of old deployments in the history section. This bothered me, and I searched for an option to delete them.

Cloudflare Workers & Pages section allow you to delete older deployment one by one. This is okay if you deploy once in a week or month. But, imagine deploying a few times in a day during the initial project phase.

Unfortunately, the Cloudflare dashboard doesn’t have an option to delete older deployments in bulk or programmatically, but there is a way!

Using the Cloudflare API, you can delete the deployments in bulk and set the crontab to do it automatically.

I’ll share a Python script which works for me and you can use too! I’ve tested this in Python 3 version.

You need a few things as prerequisites before executing the script.

Once you have the above info, use them in the below script under “Cloudflare Config” section. You also need to specify how many recent deployments you want to keep in “KEEP_DEPLOYMENTS” variable. I’ve kept it 3 as that is sufficient for me, but it can be different for you.

import requests

# Cloudflare Config
API_TOKEN = "cloudflare_api_token_here"
ACCOUNT_ID = "cloudflare_account_id_here"
PROJECT_NAME = "cloudflare_pages_app_name"
KEEP_DEPLOYMENTS = 3

BASE_URL = f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/pages/projects/{PROJECT_NAME}/deployments"

HEADERS = {
    "Authorization": f"Bearer {API_TOKEN}",
    "Content-Type": "application/json",
}

def list_deployments():
    deployments = []
    page = 1
    per_page = 25

    while True:
        response = requests.get(BASE_URL, headers=HEADERS, params={"page": page, "per_page": per_page})
        data = response.json()
        
        if not data.get("success"):
            print(f"Failed to list deployments: {data}")
            break
        
        deployments_page = data["result"]
        if not deployments_page:
            break

        deployments.extend(deployments_page)
        page += 1

    return deployments

def delete_deployment(deployment_id):
    url = f"{BASE_URL}/{deployment_id}"
    response = requests.delete(url, headers=HEADERS)
    if response.status_code == 200:
        print(f"Deleted deployment: {deployment_id}")
    else:
        print(f"Failed to delete {deployment_id}: {response.json()}")

def cleanup_old_deployments():
    deployments = list_deployments()
    deployments.sort(key=lambda d: d["created_on"], reverse=True)
    
    to_delete = deployments[KEEP_DEPLOYMENTS:]
    print(f"Found {len(deployments)} deployments. Deleting {len(to_delete)} old ones...")

    for deployment in to_delete:
        delete_deployment(deployment["id"])

if __name__ == "__main__":
    cleanup_old_deployments()

Once the file is updated, run from anywhere.

root@chandan:/opt/backup/cloudflare# python3 delete_cf_pages_old_deployment.py 
Found 8 deployments. Deleting 5 old ones...
Deleted deployment: 9295196f-ffd5-4cad-9c93-366ceb98b2b2
Deleted deployment: a8123b8a-3fb3-4d44-9795-ba61c724a8be
Deleted deployment: 4bf5535c-d6c7-402c-9995-8fea895d8252
Deleted deployment: acd44ddd-1819-47b6-912b-2249fa764e89
Deleted deployment: f1a6f852-5879-4804-975f-f49403416902
root@chandan:/opt/backup/cloudflare# 

As you can see above, it has deleted the older deployments.

Cool! 😎

To run this automatically, you can set this in crontab. The below example will run the script every day at 8 AM.

  • Open crontab using crontab -e
  • Add below line and save the cron file
0 8 * * * /usr/bin/python3 /path/delete_cf_pages_old_deployment.py.py >> /var/cf-old-dep-delete.log 2>&1

Don’t forget to update the script path to match your environment. It’s easy, isn’t it? 😀