How to clear cache of one page using cron?

Hey all (sorry for the n00b question)

Background
I’m using APO, which sadly has a bug that doesn’t clear the homepage when a new post is published.
In order to solve it, right now, I just keep the homepage without cache.
Instead, I’d like to move to having the homepage cached, but to clear it via cron every X minutes.

Question
I found out this example on which command I can run in cron:
https://api.cloudflare.com/#zone-purge-files-by-url
But I don’t understand the example.
Specifically, I don’t understand the structure of the --data argument.

Could anyone propose which cron task I should write to clear the homepage of example.com? (both http and https)

Thanks upfront!

May this is not the answer you expect but why do you want to xlear Cache by a CRON?
This is probably the worst implementation of Cache-Wipe as Cache is made to speed up your site and you are then recurringly by CRON destroying it.

You may have the problem that the Cache is not UpToDate, but then please rather find the event that changes your site-content and hook-in this event to trigger a Cache-Flush just when needed and not recurringly.

That’s a great point.
I can also think of using the API to refresh a post by hooking it to a Wordpress event.
That said, I still need the help on how to structure the API call. Any suggestions there?

Well as of the API its very well documented in the Documentation. So probably noone will give you a finished solution on what exactly you have to do to have an already finished solution.

But to guide you into the right direction I would recommend you check this APO setting mentioned here by @sdayman:

After doing this APO should catch ALL regular changes within your Application and trigger a Cache Clear automatically.
If this is not getting done you have 2 options:

  1. the change made to you site was not made within your WordPress application and therefore could not get fetched/hooked-in and will not trigger a cache-clear. Then you have to build your own little script.
  2. The Canges are normal changes within the application (e.g.: content changed by any user) then I would honestly check if the problem is related to APO and this may be a bug which occurs in combination of APO and any special Plugins you have installed.

But for Option (1.) I would recommend you first checing existing solutions on the web, like GitHub and how they clear cache. As WordPress is PHP based I would check for PHP based scripts/solutions so you do not depend on any other programming language.
Normal ShellScripts are ok aswell.

I found a good project to start with trying it:

Hope this will help you understand how it works so you can modify it to fit your needs.

1 Like

Please make sure you run the latest WP plugin v 4.4.0. I’d like to know more about the type of post you publish? Do you add it programmatically or via scheduled event? There were cases before when scheduled post didn’t clear the cache. Maybe we have more missing cases. Ideally we should fix APO cache invalidation logic instead of clear cache via crone.

Hey @yevgen ,
Thanks for the response!

My site is r-bloggers.com
I use feedwordpress to fetch posts from an RSS feed and publish them. This worked fine to refresh the homepage, but somehow stopped working some weeks ago. I’ve since moved the site to a new server with php 7.2, updated the plugin to 4.4.0 - but still it didn’t work.
Also when I use the “quick edit” to change the publication time of the post, doesn’t refresh the cache of the homepage (and I’ve checked it using incognito mode).

I’ve looked at the APO plugin. I see that the purging functions are initiated here:

And specifically, the interesting function is purgeCacheByRelevantURLs. Which is initiated in line 109 here:

Now the question is why did this stop working for me. There are several options:

  1. that somehow purgeCacheByRelevantURLs doesn’t work properly
  2. that it’s not initiated properly
  3. that the action I’m using somehow are not registered in transition_post_status properly

If I’m to guess, I’d go with option 2. But it’s hard for me to know.
A simple way to help me find out is if you would do some of the following:

  1. create a dedicated function to purge the homepage and the paginated pages (something similar to cloudflare_purge_by_url, could be called cloudflare_purge_homepage), and make it easily called externally (instead of the current init that is needed to the hook class).
    This would allow me to run something like:
    if(function_exists(‘post_syndicated_item’)){
    add_action(‘post_syndicated_item’,“cloudflare_purge_homepage”);
    }
    This would allow me to play with the priority (I see it’s currently set to 3, maybe if I try 1 or 1/10, it would work). Alternatively, I could make the action happen every X minutes, which would give me another way to solve the problem for myself.

  2. you can also expose a cloudflare_purge_by_url function, and I could use it to enter the homepage, and see if that works.

  3. You can also add an argument to control the action priority (currently it’s 3, and I’m guessing making it a smaller or larger number might solve the problem. But who knows.

  4. is there any debug log I can use? (I see that there is a debug call inside the plugin, but I don’t see where I can see it’s log)

What do you think?

(also feel free to contact me via email around this: [email protected])

And regarding the solution you proposed:

The line I want help with is:
$purge = [‘purge_everything’ => true];
Looking at here:

Could I just use:
$purge = [‘files’ => [‘www.example.com/’]]

Would this work? (since it’s not a path to a specific file, but the homepage)
Or would it purge all the sub-pages of the site as well? (I imagine not, since it’s not a prefix call)

Thanks upfront!

Could I just use:
$purge = [‘files’ => [‘www.example.com/’]]

It should work, it’s the equivalent of Cloudflare API v4 Documentation.

Or would it purge all the sub-pages of the site as well? (I imagine not, since it’s not a prefix call)

It won’t purge subpages.

I tried it, and it didn’t seem to work. What should I do next?

After reading your post again I realised you can try to revert to version 4.2.2 and check if it works for you. We did changes around post status changes in 4.3.0 and 4.4.0 which inadvertently impacted your setup.

You can always call purge API directly:

curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'

as per docs: Cloudflare API v4 Documentation.

Thanks, the point is I’ve seen this code, and tried it - and it didn’t work.
So I’m asking how would you use this code to clear the cache of either the homepage or a specific BLOG post.

e.g.:

using

curl -X POST “https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache” \ -H “X-Auth-Email: [email protected]” \ -H “X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41” \ -H “Content-Type: application/json” \ --data ‘{“url”:“http://www.example.com/”}’

Doesn’t work.

And also:

curl -X POST “https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache” \ -H “X-Auth-Email: [email protected]” \ -H “X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41” \ -H “Content-Type: application/json” \ --data ‘{“url”:“http://www.example.com/post-name”}’

Could you please LMK how to modify it so it would work?

Maybe I state the obvious but zone id, email and auth-key should be unique to your zone.

Could you please help me here, I want a specific call to remove the homepage.
In the previous email, I proposed this:

curl -X POST “https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache” \ -H “X-Auth-Email: [email protected]” \ -H “X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41” \ -H “Content-Type: application/json” \ --data ‘{“url”:“http://www.example.com/”}’

When I run this, I use the X-Auth-Email and X-Auth-Key, that are unique to my account.
I also fill --data ‘{“url”:“http://www.example.com/”}’
With my own url.

This call does not work.
Could you please write to me how this call (for the homepage of a blog, not a css or jpg file), should be done? (for example, I don’t see where I should enter a zone id in this call)

Thanks,
T

I will DM you.