wppaste
WordPress

wp_uninitialize_site( int|WP_Site $site_id ): true|WP_Error

Since
5.1.0
Source
wp-includes/ms-site.php:784
Runs the uninitialization routine for a given site.

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

Reaches the database via get_users().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–90

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

Plugin surface
2 hooks

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.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_users()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
empty($site_id)12__()
!empty($site_id)17get_site(), __()
!empty($site_id) && !wp_is_site_initialized()21get_site(), wp_is_site_initialized(), __()
!empty($site_id) && wp_is_site_initialized() && !$index77–82get_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() && !$index79–84get_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() && !$index83–88get_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() && !$index85–90get_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:

  1. apply_filters( wpmu_drop_tables )filterline 832 (+48 into the body)

    Filters the tables to drop when the site is deleted.

  2. 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

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.NotPrepared	} 	/**	 * 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.

  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.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.