Purge LSCache Manually

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 before 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.

Procedures

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

<IfModule LiteSpeed>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
RewriteRule .* - [E=Cache-Control:max-age=120]
</IfModule>

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

curl -isX PURGE https://example.com/index.php

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.

Add IP to Allow 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) .

Testing

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

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

curl -isX PURGE https://example.com/index.php
Response:
HTTP/1.1 200 Purged
Connection: close
Content-Length: 0
Date: Tue, 20 Aug 2019 19:51:40 GMT
Server: LiteSpeed
Alt-Svc: quic=":443"; ma=2592000; v="39,43,46", h3-22=":443"; ma=2592000

Check the header of https://example.com/index.php 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.

Possible issue

  • Please check Add IP to Allow List if you see HTTP/1.1 405 Method Not Allowed
  • Sometimes killall -9 lsphp can help the case