wpmu_delete_user( int $id ): bool
- Since
- 3.0.0
- Source
wp-admin/includes/ms.php:145
Description
This function:
- Deletes all posts (of all post types) authored by the user on all sites on the network
- Deletes all links owned by the user on all sites on the network
- Removes the user from all sites on the network
- Deletes the user from the database
Compatibility
- WordPress
- since 3.0.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
$idint- The user ID.
Return value
bool- True if the user was deleted, false otherwise.
Performance profile
How much work a call to wpmu_delete_user() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 5–66
- Plugin surface
- 2 hooks
- Called by
- 0
Reaches the database via ->get_col().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 128.
Third-party callbacks on 'wpmu_delete_user', 'deleted_user' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()called directly - sqldatabase query
->get_col()called directly - optionnetwork option
get_site_option()one call below wpmu_delete_user() - cacheobject cache
wp_cache_switch_to_blog()one call below wpmu_delete_user() - querycontent query
get_post()one call below wpmu_delete_user()
Further down the call graph this can also reach serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpmu_delete_user() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$id | 5 | none |
$id && !->exists() | 14 | ->exists() |
$id && ->exists() && $_super_admins | 21 | ->exists(), get_super_admins() |
$id && ->exists() && !$_super_admins | 62–66 | ->exists(), get_super_admins(), do_action(), get_blogs_of_user(), ->prepare(), ->get_col(), ID(), clean_user_cache(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 128 | 5–66 | 13 | |
| 8.5 | 128 | 5–66 | 13 | |
| 8.4 | 128 | 5–66 | 13 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 133 | 7–71 | 13 | |
| 8.2 | 133 | 7–71 | 13 | |
| 8.1 | 133 | 7–71 | 13 | |
| 7.4 | 133 | 7–71 | 13 |
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 · 2
2 hooks fire while wpmu_delete_user() runs, in this order:
- do_action( wpmu_delete_user )actionline 174 (+29 into the body)
Fires before a user is deleted from the network.
- do_action( deleted_user )actionline 211 (+66 into the body)
Fires immediately after a user is deleted from the site.
Uses · 11
- get_super_admins()Retrieves a list of super admins.
- do_action()Calls the callback functions that have been added to an action hook.
- get_blogs_of_user()Gets the sites a user belongs to.
- switch_to_blog()Switches the current blog.
- remove_user_from_blog()Removes a user from a blog.
- wp_delete_post()Trashes or deletes a post or page.
- wp_delete_link()Deletes a specified link from the database.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
- delete_metadata_by_mid()Deletes metadata by meta ID.
- clean_user_cache()Cleans all user caches.
- WP_User::__construct()Constructor.
Source code
function wpmu_delete_user( $id ) { global $wpdb; if ( ! is_numeric( $id ) ) { return false; } $id = (int) $id; $user = new WP_User( $id ); if ( ! $user->exists() ) { return false; } // Global super-administrators are protected, and cannot be deleted. $_super_admins = get_super_admins(); if ( in_array( $user->user_login, $_super_admins, true ) ) { return false; } /** * Fires before a user is deleted from the network. * * @since MU (3.0.0) * @since 5.5.0 Added the `$user` parameter. * * @param int $id ID of the user about to be deleted from the network. * @param WP_User $user WP_User object of the user about to be deleted from the network. */ do_action( 'wpmu_delete_user', $id, $user ); $blogs = get_blogs_of_user( $id ); if ( ! empty( $blogs ) ) { foreach ( $blogs as $blog ) { switch_to_blog( $blog->userblog_id ); remove_user_from_blog( $id, $blog->userblog_id ); $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); foreach ( (array) $post_ids as $post_id ) { wp_delete_post( $post_id ); } // Clean links. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); if ( $link_ids ) { foreach ( $link_ids as $link_id ) { wp_delete_link( $link_id ); } } restore_current_blog(); } } $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); foreach ( $meta as $mid ) { delete_metadata_by_mid( 'user', $mid ); } $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); clean_user_cache( $user ); /** This action is documented in wp-admin/includes/user.php */ do_action( 'deleted_user', $id, null, $user ); return true;}Changelog
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/ms.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.