API: Purge: How to troubleshoot when it fails?

I’m trying to programatically purge urls but can’t get it to work. It seems to work and the API returns a success message, but I believe the page isn’t actually being purged.

Doing this in Python and it is roughly like this:

            # get the cloudflare api url
            cloudflare_api_url = 'https://api.cloudflare.com/client/v4/zones/' + \
                zones_list[domain]['zoneid'] + '/purge_cache'
            # set the headers for the email and the api_key
            headers = {'X-Auth-Email': email, 'X-Auth-Key': key}

            # set the data for the request to purge just this url
            data = {'files': [clearcloudflare_url_list_by_domain]}

            # make the request
            r = uncached_session.post(cloudflare_api_url, headers=headers, json=data)

            if r.status_code != 200:
                print('\nCloudFlare purge request failed for {} :  {}'.format(domain,r.text))

                if verbose:
                    print('\nCloudFlare purge request for {} successful'.format(domain))

After doing this when I load the URL it is still “hit” and shows the old content.

I understand from the documentation (https://developers.cloudflare.com/cache/how-to/purge-cache/) that there are cases where this won’t work due to headers and cache keys, but I’m not sure how this applies. These are Wordpress pages and I am not explicitly setting any special headers or keys.

How/where should I like to troubleshoot this? Is there a way to find the headers/keys that may be preventing it from working? I understand that I’ll need to pass the url/headers to the purge in such a case.


My bad; this line is in error. Should be:

data = {'files': clearcloudflare_url_list_by_domain}

Was mislead because even though I was incorrectly passing a list-of-lists instead of a list it gave me a “success: true” result.

After examining the raw request I noticed the error when comparing with a valid one created with curl.

The example documentation shows passing both a simple URL and a url-with-headers and it turns out that for my simple case the simple URL is fine, no need to include headers.