Back to Community

How to Purge Private Cache for a Specific User in LiteSpeed Cache

12 threads Sep 7, 2025 PluginLitespeed cache

Content

Managing cached content for logged-in users is a common challenge for WordPress site administrators. A frequent question that arises is how to clear the private cache for a single, specific user without affecting the cache for all other users. This is particularly important on sites where user-specific content, like role-based product visibility or private messages, updates frequently.

Why You Can't Purge a Single User's Cache by Default

Based on community discussions, the LiteSpeed Cache plugin's native functionality does not allow for purging a single user's private cache. The private cache is typically labeled with a general "private" tag. This means the litespeed_purge_private hook purges all private cache for every logged-in user simultaneously. For many sites, this "all or nothing" approach is inefficient and can negatively impact performance if done frequently.

A Workaround: Tagging and Purging by User ID

While not a built-in feature, a solution exists by manually tagging a user's cache and then purging by that specific tag. This method was shared by a community member and involves adding two code snippets to your theme's functions.php file or a custom plugin.

Step 1: Add a User-Specific Cache Tag

The first function adds a unique tag to every page served to a logged-in user. This tag identifies the cache object as belonging to that specific user.

function add_user_cache_tag() {
    if ( is_user_logged_in() && defined('LSCWP_V')) {
        $user_id = get_current_user_id();
        do_action( 'litespeed_tag_add', 'user_id_' . $user_id );
    }
}
add_action( 'init', 'add_user_cache_tag' );

Step 2: Purge Cache by the User Tag

When you need to clear the cache for a specific user (e.g., after updating their profile or user role), you can trigger a purge using their unique tag.

$user_id = 123; // Replace 123 with the actual user ID
do_action( 'litespeed_purge', 'user_id_' . $user_id );

You would integrate this purge action into your own code, perhaps hooking it into events like profile_update or set_user_role.

Important Considerations and an Alternative

  • Testing is Crucial: This is a community-provided workaround. Always test this code on a staging site before deploying it to production to ensure compatibility with your specific setup.
  • Alternative Approach - Disable Caching for Logged-in Users: If purging individual user caches seems too complex for your needs, a simpler alternative is to disable caching for logged-in users entirely. This can be done in the LiteSpeed Cache settings under Cache -> Cache -> Cache Logged-in Users. Alternatively, you can exclude specific user roles from being cached under Cache -> Excludes -> Do not cache roles. This ensures users always see dynamic, uncached content, which is ideal for sites with highly personalized pages like forums or membership portals.

This approach provides a powerful way to manage private cache more granularly, enhancing site performance while ensuring users see up-to-date content.

Related Support Threads Support