Title: wp_uninitialize_site
Published: February 22, 2019
Last modified: February 24, 2026

---

# wp_uninitialize_site( int|WP_Site $site_id ): true|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#wp--skip-link--target)

Runs the uninitialization routine for a given site.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#description)󠁿

This process includes dropping the site’s database tables and deleting its uploads
directory.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#parameters)󠁿

 `$site_id`int|[WP_Site](https://developer.wordpress.org/reference/classes/wp_site/)
required

Site ID or object.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#return)󠁿

 true|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) True
on success, or error object on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#source)󠁿

    ```php
    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 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;
    				}

    				if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
    					$stack[] = $dir . DIRECTORY_SEPARATOR . $file;
    				} elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
    					@unlink( $dir . DIRECTORY_SEPARATOR . $file );
    				}

    				$file = @readdir( $dh );
    			}
    			@closedir( $dh );
    		}
    		++$index;
    	}

    	$stack = array_reverse( $stack ); // Last added directories are deepest.
    	foreach ( (array) $stack as $dir ) {
    		if ( $dir !== $top_dir ) {
    			@rmdir( $dir );
    		}
    	}

    	// phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
    	if ( $switch ) {
    		restore_current_blog();
    	}

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/ms-site.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/ms-site.php#L784)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/ms-site.php#L784-L892)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#hooks)󠁿

 [apply_filters( ‘wpmu_delete_blog_upload_dir’, string $basedir, int $site_id )](https://developer.wordpress.org/reference/hooks/wpmu_delete_blog_upload_dir/)

Filters the upload base directory to delete when the site is deleted.

 [apply_filters( ‘wpmu_drop_tables’, string[] $tables, int $site_id )](https://developer.wordpress.org/reference/hooks/wpmu_drop_tables/)

Filters the tables to drop when the site is deleted.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_is_site_initialized()](https://developer.wordpress.org/reference/functions/wp_is_site_initialized/)`wp-includes/ms-site.php` |

Checks whether a site is initialized.

  | 
| [get_site()](https://developer.wordpress.org/reference/functions/get_site/)`wp-includes/ms-site.php` |

Retrieves site data given a site ID or site object.

  | 
| [wp_get_upload_dir()](https://developer.wordpress.org/reference/functions/wp_get_upload_dir/)`wp-includes/functions.php` |

Retrieves uploads directory information.

  | 
| [get_users()](https://developer.wordpress.org/reference/functions/get_users/)`wp-includes/user.php` |

Retrieves list of users matching criteria.

  | 
| [remove_user_from_blog()](https://developer.wordpress.org/reference/functions/remove_user_from_blog/)`wp-includes/ms-functions.php` |

Removes a user from a blog.

  | 
| [switch_to_blog()](https://developer.wordpress.org/reference/functions/switch_to_blog/)`wp-includes/ms-blogs.php` |

Switches the current blog.

  | 
| [restore_current_blog()](https://developer.wordpress.org/reference/functions/restore_current_blog/)`wp-includes/ms-blogs.php` |

Restores the current blog, after calling [switch_to_blog()](https://developer.wordpress.org/reference/functions/switch_to_blog/) .

  | 
| [wpdb::query()](https://developer.wordpress.org/reference/classes/wpdb/query/)`wp-includes/class-wpdb.php` |

Performs a database query, using current database connection.

  | 
| [wpdb::tables()](https://developer.wordpress.org/reference/classes/wpdb/tables/)`wp-includes/class-wpdb.php` |

Returns an array of WordPress tables.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [get_current_blog_id()](https://developer.wordpress.org/reference/functions/get_current_blog_id/)`wp-includes/load.php` |

Retrieves the current site ID.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 8 more](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_uninitialize_site/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.1.0](https://developer.wordpress.org/reference/since/5.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_uninitialize_site%2F)
before being able to contribute a note or feedback.