Cron Job API Cache Purge Error

Dear Cloudflare community,

i need to purge my site cache via cron job. I tried using the following scripts:

#!/bin/bash
# CACHE PURGE script for CLOUDFLARE
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache" \
     -H "X-Auth-Email: USER_MAIL" \
     -H "Authorization: Bearer API_TOKEN" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

and:

#!/bin/bash
# CACHE PURGE script for CLOUDFLARE
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache" \
     -H "X-Auth-Email: USER_MAIL" \
     -H "X-Auth-Key: GLOBAL_API_KEY" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

And I tried to run these scripts via the following cron job on cPanel:

/usr/local/cpanel/bin/jailshell /home/site/folder/cloudflare-script.sh

The result of the tested cron jobs always gives me the following error:

{“success”:false,“errors”:[{“code”:9106,“message”:“Missing X-Auth-Key, X-Auth-Email or Authorization headers”}]}
curl: (3) Illegal characters found in URL
/home/site/folder/cloudflare-script.sh: line 4: -H: command not found
/home/site/folder/cloudflare-script.sh: line 5: -H: command not found
/home/site/folder/cloudflare-script.sh: line 6: -H: command not found
/home/site/folder/cloudflare-script.sh: line 7: --data: command not found

I am attaching the API Token settings. Could you give me a hand to solve the problem? A thousand thanks.

Notice how it does not recognise the -H argument. Something in your Bash script appears to be broken, you are not sending any of the parameters along with your request.

Possibly something missing in your call or your ZONE_ID variable breaks it. Classic script debugging I am afraid :slight_smile:.

1 Like

I simply copied the script from the Cloudflare documentation at the link below:

https://api.cloudflare.com/#zone-purge-all-files

Then I added above:

#!/bin/bash
# CACHE PURGE script for CLOUDFLARE

I replaced the variables with my account data and ran the script via cron. I really don’t understand what the problem could be.

As I said.

Something will be wrong with the way you call cURL or your formatting. I am afraid shell scripting is a bit beyond the scope of the forum here and best discussed at StackExchange or Reddit.

1 Like

Thanks a lot Sandro, I solved the problem by putting all the curl command in one line as below:

#!/bin/bash
# CACHE PURGE script for CLOUDFLARE
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache" -H "X-Auth-Email: USER_MAIL" -H "Authorization: Bearer API_TOKEN" -H "Content-Type: application/json" --data '{"purge_everything":true}'

Now the cron gives me the following result:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    25    0     0  100    25      0     22  0:00:01  0:00:01 --:--:--    22
100   145    0   120  100    25     98     20  0:00:01  0:00:01 --:--:--    98
{
  "result": {
    "id": "RESULT_ID"
  },
  "success": true,
  "errors": [],
  "messages": []
}

I think the script worked, is there any way to verify that the cache on Cloudflare has actually been cleared? Thanks.

If the first request does not show a cache hit any more, then you were not served from the cache.

1 Like

Perfect, I checked, thanks again and have a nice day.

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