Pages Deployments API doesn't support per_page query parameter

In the Cloudflare API documentation (https://developers.cloudflare.com/fundamentals/api/how-to/make-api-calls/#pagination), it states that the page/per_page query params are used to pagination through responses. However, the API for Pages Deployments does not accept the per_page parameter.

I’m using the API to bulk-delete preview deployments (aliases) that are greater than one month old. However, given that this is a long-lived project with many developers, I have to send a ton of API requests to paginate through all of the deployments with the default per_page of 25.

When attempting to increase per_page to 50, I receive the following response:

  curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/pages/projects/{project}/deployments?per_page=50" \
       --header "Content-Type: application/json" \
       --header "Authorization: Bearer {api_token}"

  {
    "result": null,
    "success": false,
    "errors": [
      {
        "code": 8000024,
        "message": "Invalid list options provided. Review the `page` or `per_page` parameter."
      }
    ],
    "messages": []
  }

This is not documented in Get deployments (https://developers.cloudflare.com/api/operations/pages-deployment-get-deployments) API documentation.

Am I missing something here, or is there an easier API that I can use to delete preview aliases for old builds that are no longer relevant? Support for the per_page parameter would be very much appreciated.

We only allow a max of 25 at a time. per_page is supported just up to a max of 25.

1 Like

Got it. Does Pages support an alternative for cleaning up old preview aliases/deployments? Or is my method correct and I just need to deal with a page size of 25.

This is the best way today

1 Like

Wrote a shell script for it :wink:

#docs: "https://developers.cloudflare.com/api/operations/pages-project-create-project"
cf_list_projects() {
  local CF_ALL_PAGES_FILE="./cf_all_pages_projects.json"
  local PAGE=1

  # Initialize an empty array to store results from all pages
  local RESULTS=()

  while true; do
    # Make a GET request to the API and save the response in a temporary file
    local TEMP_FILE="./cf_list_pages_projects_page_$PAGE.json"
    curl --request GET \
      --url "$CF_URL_PAGES?page=$PAGE" \
      -H "Authorization: Bearer $CF_X_KEY" \
      -H "Content-Type: application/json" > "$TEMP_FILE"

    # Check if the request was successful
    if [ $? -ne 0 ]; then
      echo "Failed to retrieve data from Cloudflare API."
      return 1
    fi

    # Extract the 'result' array from the JSON response and append it to the RESULTS array
    jq -c '.result[]' "$TEMP_FILE" >> "$CF_ALL_PAGES_FILE"

    # Increment the page number for the next request
    PAGE=$((PAGE + 1))

    # Check if there are more pages to fetch
    local TOTAL_PAGES=$(jq -r '.result_info.total_pages' "$TEMP_FILE")
    if [ $PAGE -gt $TOTAL_PAGES ]; then
      break
    fi
  done

  echo "All pages have been successfully merged into $CF_ALL_PAGES_FILE"
}

#cf_list_projects

Also did some expansion of the results.

Note some of the things are not the same for various ways of deploying pages.
So check if each of these actually exists Notably sometimes its last_stage incited of latest_deployment

  let {
        id,
        name,
        subdomain,
        domains,
        source,
        build_config,
        deployment_configs,
        latest_deployment,
        canonical_deployment,
        created_on,
        production_branch,
        production_script_name,
        preview_script_name
    } = p;

    // console.log(Object.keys(p))
    let gitlab_url = ""
    if (source && source.config && source.type === "gitlab") {
        gitlab_url = `https://gitlab.com/${source.config.owner}/${source.config.repo_name}#${source.config.production_branch}`
    }

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.