wp_uninitialize_site( int|WP_Site $site_id ): true|WP_Error
- Since
- 5.1.0
- Source
wp-includes/ms-site.php:784
Description
This process includes dropping the site's database tables and deleting its uploads directory.
Compatibility
- WordPress
- since 5.1.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
$site_idint|WP_Site- Site ID or object.
Return value
true|WP_Error- True on success, or error object on failure.
Performance profile
How much work a call to wp_uninitialize_site() 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
- 12–90
- Plugin surface
- 2 hooks
- Called by
- 0
Reaches the database via get_users().
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 205.
Third-party callbacks on 'wpmu_drop_tables', 'wpmu_delete_blog_upload_dir' 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
- querycontent query
get_users()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_switch_to_blog()one call below wp_uninitialize_site()
Further down the call graph this can also reach option, 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 · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_uninitialize_site() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($site_id) | 12 | __() |
!empty($site_id) | 17 | get_site(), __() |
!empty($site_id) && !wp_is_site_initialized() | 21 | get_site(), wp_is_site_initialized(), __() |
!empty($site_id) && wp_is_site_initialized() && !$index | 77–82 | get_site(), wp_is_site_initialized(), blog_id(), get_current_blog_id(), wp_get_upload_dir(), ->tables(), apply_filters(), apply_filters(), rtrim(), array_reverse() |
!empty($site_id) && wp_is_site_initialized() && !$index | 79–84 | get_site(), wp_is_site_initialized(), blog_id(), get_current_blog_id(), wp_get_upload_dir(), ->tables(), apply_filters(), apply_filters(), rtrim(), array_reverse(), restore_current_blog() |
!empty($site_id) && wp_is_site_initialized() && !$index | 83–88 | get_site(), wp_is_site_initialized(), blog_id(), get_current_blog_id(), switch_to_blog(), wp_get_upload_dir(), ->tables(), apply_filters(), apply_filters(), rtrim(), array_reverse() |
!empty($site_id) && wp_is_site_initialized() && !$index | 85–90 | get_site(), wp_is_site_initialized(), blog_id(), get_current_blog_id(), switch_to_blog(), wp_get_upload_dir(), ->tables(), apply_filters(), apply_filters(), rtrim(), array_reverse(), restore_current_blog() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 205 instructions, 12–90 executed per call, 20 branches. The work does not change between versions.
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 wp_uninitialize_site() runs, in this order:
- apply_filters( wpmu_drop_tables )filterline 832 (+48 into the body)
Filters the tables to drop when the site is deleted.
- apply_filters( wpmu_delete_blog_upload_dir )filterline 846 (+62 into the body)
Filters the upload base directory to delete when the site is deleted.
Uses · 11
- __()Retrieves the translation of $text.
- get_site()Retrieves site data given a site ID or site object.
- wp_is_site_initialized()Checks whether a site is initialized.
- get_users()Retrieves list of users matching criteria.
- remove_user_from_blog()Removes a user from a blog.
- get_current_blog_id()Retrieves the current site ID.
- switch_to_blog()Switches the current blog.
- wp_get_upload_dir()Retrieves uploads directory information.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
- WP_Error::__construct()Initializes the error.
Source code
function wp_uninitialize_site( $site_id ) { global $wpdb; if ( empty( $site_id ) ) { return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); } $site = get_site( $site_id ); if ( ! $site ) { return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) ); } if ( ! wp_is_site_initialized( $site ) ) { return new WP_Error( 'site_already_uninitialized', __( 'The site appears to be already uninitialized.' ) ); } $users = get_users( array( 'blog_id' => $site->id, 'fields' => 'ids', ) ); // Remove users from the site. if ( ! empty( $users ) ) { foreach ( $users as $user_id ) { remove_user_from_blog( $user_id, $site->id ); } } $switch = false; if ( get_current_blog_id() !== $site->id ) { $switch = true; switch_to_blog( $site->id ); } $uploads = wp_get_upload_dir(); $tables = $wpdb->tables( 'blog' ); /** * Filters the tables to drop when the site is deleted. * * @since MU (3.0.0) * * @param string[] $tables Array of names of the site tables to be dropped. * @param int $site_id The ID of the site to drop tables for. */ $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id ); foreach ( (array) $drop_tables as $table ) { $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared } /** * Filters the upload base directory to delete when the site is deleted. * * @since MU (3.0.0) * * @param string $basedir Uploads path without subdirectory. See {@see wp_upload_dir()}. * @param int $site_id The site ID. */ $dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id ); $dir = rtrim( $dir, DIRECTORY_SEPARATOR ); $top_dir = $dir; $stack = array( $dir ); $index = 0; while ( $index < count( $stack ) ) { // Get indexed directory from stack. $dir = $stack[ $index ]; // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged $dh = @opendir( $dir ); if ( $dh ) { $file = @readdir( $dh ); while ( false !== $file ) { if ( '.' === $file || '..' === $file ) { $file = @readdir( $dh ); continue;Changelog
Introduced in 5.1.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-includes/ms-site.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.