wppaste
WordPress

clean_user_cache( WP_User|int $user )

Since
3.0.0, 4.4.0, 6.2.0
Source
wp-includes/user.php:1940
Cleans all user caches.

Compatibility

WordPress
since 6.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$userWP_User|int
User object or ID to be cleaned from the cache

Performance profile

How much work a call to clean_user_cache() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Only touches the object cache, via wp_cache_delete().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–52

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 53.

Plugin surface
1 hook

Third-party callbacks on 'clean_user_cache' run inside this call, and their cost is not bounded by anything here.

Called by
11

11 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_delete()called directly
  • hookthird-party callbacksdo_action()called directly

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost clean_user_cache() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!->exists()7–11->exists()
->exists() && empty($user)42–46->exists(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), wp_cache_set_users_last_changed(), do_action()
->exists() && !empty($user)48–52->exists(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), wp_cache_set_users_last_changed(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev537–523
8.5537–523
8.4537–5232 fewer instructions than PHP 8.3
8.3559–543
8.2559–543
8.1559–543
7.4559–543

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while clean_user_cache() runs, in this order:

  1. do_action( clean_user_cache )actionline 1968 (+28 into the body)

    Fires immediately after the given user's cache is cleaned.

Uses · 4

Used by · 11

Source code

function clean_user_cache( $user ) {	if ( is_numeric( $user ) ) {		$user = new WP_User( $user );	} 	if ( ! $user->exists() ) {		return;	} 	wp_cache_delete( $user->ID, 'users' );	wp_cache_delete( $user->user_login, 'userlogins' );	wp_cache_delete( $user->user_nicename, 'userslugs' ); 	if ( ! empty( $user->user_email ) ) {		wp_cache_delete( $user->user_email, 'useremail' );	} 	wp_cache_delete( $user->ID, 'user_meta' );	wp_cache_set_users_last_changed(); 	/**	 * Fires immediately after the given user's cache is cleaned.	 *	 * @since 4.4.0	 *	 * @param int     $user_id User ID.	 * @param WP_User $user    User object.	 */	do_action( 'clean_user_cache', $user->ID, $user );}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.2.0
User metadata caches are now cleared.from the docblock
4.4.0
'clean_user_cache' action was added.from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.