Using WordPress ‘clean_user_cache()’ PHP function

The clean_user_cache() WordPress PHP function cleans all user caches.

Usage

To use the function, simply pass the user object or ID to it:

clean_user_cache( $user );

Parameters

  • $user (WP_User|int) (Required): User object or ID to be cleaned from the cache.

More information

See WordPress Developer Resources: clean_user_cache()

Examples

Clean Cache for a Specific User by ID

Clean the cache for a user with ID 5:

$user_id = 5;
clean_user_cache( $user_id );

Clean Cache for the Current User

Clean the cache for the currently logged-in user:

$current_user = wp_get_current_user();
clean_user_cache( $current_user );

Clean Cache for a User by Username

Clean the cache for a user with the username ‘john_doe’:

$username = 'john_doe';
$user = get_user_by( 'login', $username );
clean_user_cache( $user );

Clean Cache for a User by Email

Clean the cache for a user with the email ‘[email protected]’:

$email = '[email protected]';
$user = get_user_by( 'email', $email );
clean_user_cache( $user );

Clean Cache for All Users

Clean the cache for all users on the site:

$all_users = get_users();
foreach ( $all_users as $user ) {
    clean_user_cache( $user->ID );
}