Purging LSCache Without a Plugin

If you are using a LiteSpeed Cache plugin, then you should not have to worry about manually purging the cache when content changes on your site. The plugin is meant to handle such situations automatically. Additionally, there is usually a “Purge” button available if manual management is required. We strongly recommend using a plugin if it is available. Check our List of LSCache plugins to see if one is available for your application.

If there is no plugin for your application, and you are using LiteSpeed Cache with rewrite rules, then you may find purging to be a challenge.

This is how LSCache works when you are using it without a plugin:

Everything is cached. Some URLs or particular conditions may be excluded, but by default everything is cached for a short period of time (for instance, two minutes). After that time, the cache expires and pages will need to be re-cached again. If any content changes befor the cache expires, the page will not be purged. Changes will not be reflected in the frontend until the cache expires naturally.

This kind of delay is fine for many cases, particularly if the cache expiration TTL is set to a very short time. However, if your TTL is very long, or if an urgent update cannot wait for the cache to expire, a mechanism for purging the cache is necessary. As of LiteSpeed Web Server v4.1.12, a cache PURGE operation is supported to accommodate cache manipulation on demand.

Let's assume you've set up rewrite rules like the following in .htaccess:

  <IfModule LiteSpeed>
  RewriteEngine On
  RewriteRule .* - [E=Cache-Control:max-age=120]

These rules enable cache for everything for two minutes. The RewriteCond for %{REQUEST_METHOD} is optional in general, but if do use it, and you want the ability to purge the cache, you will need to add PURGE to it.

Change the rewrite rules, like so:

<IfModule LiteSpeed>
  RewriteEngine On
  RewriteRule .* - [E=Cache-Control:max-age=120]

The cache purge can be initiated through the following curl command:

curl -i -X PURGE http://example.com/

Please note that if the homepage http://example.com/index.php needs to be purged, the trailling / is required. The follwing command won't work:

curl -i -X PURGE http://example.com

The above curl command can be run either within the server or from a remote server. If a purge curl command is to be run from a remote server, the remote server IP will need to be added to LiteSpeed Web Server's Trusted IP list. Navigate to LSWS Admin Console > Configuration > Server > Security, and append the source IP with a trailing T to Allow List. (The T signifies the IP is Trusted. It should look like ALL, x.x.x.xT) .

Visit http://example.com/ through the browser and ensure X-Litespeed-Cache: hit is set.

Then, using the above curl command, purge the homepage either within the server or from a remote server, like so:

curl -i -X PURGE http://example.com/

HTTP/1.1 200 Purged
Content-Length: 0
Date: Fri, 28 Jun 2019 18:26:46 GMT
Server: LiteSpeed
Alt-Svc: quic=":443"; ma=2592000; v="35,39,43,44"
Connection: close

Check the header of http://example.com/ again, and it should show X-Litespeed-Cache: miss instead of X-Litespeed-Cache: hit. This means the cache was purged successfully. If you hit the URL the second time, you should see X-Litespeed-Cache: hit again.

  • Admin
  • Last modified: 2019/07/02 13:50
  • by Lisa Clarke