Chapter 8. How-to Guides

Delete Cached Pages from Another System Pro

A deployment script, a publishing tool or another site can tell JCH Optimize to delete cached pages, so visitors get the new pages straight away. It sends your site one of two requests:

  • delete every cached page;

  • delete the cached versions of the pages whose addresses it lists, up to 100 in a request.

Either one also deletes the pages' static files (Serve Cached Pages as Static Files) and clears them at Cloudflare when Enable Cloudflare Purge is on. The combined CSS and JavaScript files are kept: only Delete Cache removes those.

The request is made in the name of one of your site's users, who must be allowed to clear the cache. Create a user for this rather than using your own account, so you can switch it off without locking yourself out.

Joomla

JCH Optimize uses Joomla's Web Services. Installing the Pro package enables the Web Services - JCH Optimize plugin, which adds the two addresses below.

  1. Choose the user. They need Web Services Login in System → Global Configuration → Permissions, and JCH Optimize's Access Administration Interface and Clear Cache permissions (see the section called “Permissions Tab”). Super Users have all three.

  2. Edit the user in Users → Manage, and on the Joomla API Token tab set Active to Yes and save. Open the tab again and copy the Token. If the tab isn't there, the user's group isn't in the Allowed User Groups of the User - Joomla API Token plugin, which allows only Super Users by default.

  3. Send the requests with the token in an X-Joomla-Token header. To delete every cached page:

    $ curl -X DELETE -H "X-Joomla-Token: token" https://www.example.com/api/index.php/v1/jchoptimize/pagecache

    To delete the cached versions of two pages:

    $ curl -X POST -H "X-Joomla-Token: token" -H "Content-Type: application/json" -d '{"urls":["https://www.example.com/blog","https://www.example.com/blog/my-article"]}' https://www.example.com/api/index.php/v1/jchoptimize/pagecache/purge

The answer is a JSON document. For the listed pages, its data.attributes.purged is the number of cached versions that were deleted; for every page, data.attributes.deleted is true.

To switch this off, disable the Web Services - JCH Optimize plugin, or set the user's token to inactive.

WordPress

JCH Optimize adds two addresses to WordPress's REST API.

  1. Choose the user. They must be able to clear the page cache: an Editor or an Administrator, or a user given the jch_clear_page_cache capability with a role editor plugin.

  2. Edit the user in Users → All Users, and under Application Passwords enter a name, click Add Application Password and copy the password. WordPress shows it only once.

  3. Send the requests with the user's name and the application password. To delete every cached page:

    $ curl -X DELETE --user "username:application password" https://www.example.com/wp-json/jch-optimize/v1/page-cache

    To delete the cached versions of two pages:

    $ curl -X POST --user "username:application password" -H "Content-Type: application/json" -d '{"urls":["https://www.example.com/blog/","https://www.example.com/my-post/"]}' https://www.example.com/wp-json/jch-optimize/v1/page-cache/purge

The answer is JSON: {"urls": [...], "purged": 2} for the listed pages, where purged is the number of cached versions that were deleted, and {"deleted": true} for every page.

To switch this off, revoke the application password on the user's profile.

Give each page's full address, starting with http:// or https://. A page's cached versions are found by its path, so one address covers the page on every host name and scheme your site answers to, and its paginated versions. A purged of 0 means the page wasn't in the cache, which isn't an error.

When a request is refused
Status: 400
Meaning: The list of addresses is missing, isn't a list, has more than 100 addresses, or one of them isn't a full address.
Status: 401
Meaning: The token or password is missing or wrong, or on Joomla the user hasn't got Web Services Login.
Status: 403
Meaning: The user isn't allowed to clear the cache.
Status: 404
Meaning: The address isn't there: check it, and on Joomla that the Web Services - JCH Optimize plugin is enabled. The free version doesn't have it.