wppaste
WordPress

wp_delete_site( int $site_id ): WP_Site|WP_Error

Since
5.1.0
Source
wp-includes/ms-site.php:212
Deletes a site from the database.

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
ID of the site that should be deleted.

Return value

WP_Site|WP_Error
The deleted site object on success, or error object on failure.

Performance profile

How much work a call to wp_delete_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 ->delete().

Scaling
Scales with input

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

Instructions
12–77

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

Plugin surface
5 hooks

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

Called by
1

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

What it touches

  • hookthird-party callbacksdo_action()called directly
  • sqldatabase query->delete()called directly
  • optionnetwork optionget_network_option()one call below wp_delete_site()
  • cacheobject cachewp_cache_delete()one call below wp_delete_site()

Further down the call graph this can also reach serialize, query 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_delete_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) && !empty($errors)20get_site(), do_action()
!empty($site_id) && empty($errors) && !is_site_meta_supported()55get_site(), do_action(), do_action(), is_site_meta_supported(), blog_id(), __(), ->delete()
!empty($site_id) && empty($errors) && !is_site_meta_supported()60get_site(), do_action(), do_action(), is_site_meta_supported(), blog_id(), clean_blog_cache(), do_action()
!empty($site_id) && empty($errors) && is_site_meta_supported()71–72get_site(), do_action(), do_action(), is_site_meta_supported(), ->prepare(), ->get_col(), blog_id(), __(), ->delete()
!empty($site_id) && empty($errors) && is_site_meta_supported()76–77get_site(), do_action(), do_action(), is_site_meta_supported(), ->prepare(), ->get_col(), blog_id(), clean_blog_cache(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 110 instructions, 12–77 executed per call, 7 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 · 5

5 hooks fire while wp_delete_site() runs, in this order:

  1. do_action( wp_validate_site_deletion )actionline 237 (+25 into the body)

    Fires before a site should be deleted from the database.

  2. do_action( delete_blog )action_deprecatedline 252 (+40 into the body)

    Fires before a site is deleted.

  3. do_action( wp_uninitialize_site )actionline 261 (+49 into the body)

    Fires when a site's uninitialization routine should be executed.

  4. do_action( wp_delete_site )actionline 283 (+71 into the body)

    Fires once a site has been deleted from the database.

  5. do_action( deleted_blog )action_deprecatedline 294 (+82 into the body)

    Fires after the site is deleted from the network.

Uses · 8

Used by · 1

Source code

function wp_delete_site( $site_id ) {	global $wpdb; 	if ( empty( $site_id ) ) {		return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );	} 	$old_site = get_site( $site_id );	if ( ! $old_site ) {		return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );	} 	$errors = new WP_Error(); 	/**	 * Fires before a site should be deleted from the database.	 *	 * Plugins should amend the `$errors` object via its `WP_Error::add()` method. If any errors	 * are present, the site will not be deleted.	 *	 * @since 5.1.0	 *	 * @param WP_Error $errors   Error object to add validation errors to.	 * @param WP_Site  $old_site The site object to be deleted.	 */	do_action( 'wp_validate_site_deletion', $errors, $old_site ); 	if ( ! empty( $errors->errors ) ) {		return $errors;	} 	/**	 * Fires before a site is deleted.	 *	 * @since MU (3.0.0)	 * @deprecated 5.1.0	 *	 * @param int  $site_id The site ID.	 * @param bool $drop    True if site's table should be dropped. Default false.	 */	do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.1.0' ); 	/**	 * Fires when a site's uninitialization routine should be executed.	 *	 * @since 5.1.0	 *	 * @param WP_Site $old_site Deleted site object.	 */	do_action( 'wp_uninitialize_site', $old_site ); 	if ( is_site_meta_supported() ) {		$blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $old_site->id ) );		foreach ( $blog_meta_ids as $mid ) {			delete_metadata_by_mid( 'blog', $mid );		}	} 	if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {		return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );	} 	clean_blog_cache( $old_site ); 	/**	 * Fires once a site has been deleted from the database.	 *	 * @since 5.1.0	 *	 * @param WP_Site $old_site Deleted site object.	 */	do_action( 'wp_delete_site', $old_site ); 	/**	 * Fires after the site is deleted from the network.	 *	 * @since 4.8.0	 * @deprecated 5.1.0	 *	 * @param int  $site_id The site ID.

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.