wpmu_delete_blog

The timeline below displays how wordpress function wpmu_delete_blog has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Deletes a site.
 *
 * @since 3.0.0
 * @since 5.1.0 Use wp_delete_site() internally to delete the site row from the database.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    $blog_id = (int) $blog_id;
    $switch = false;
    if (get_current_blog_id() !== $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 === $blog_id || is_main_site($blog_id) || $blog->path === $current_network->path && $blog->domain === $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        wp_delete_site($blog_id);
    } else {
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('delete_blog', array($blog_id, false), '5.1.0');
        $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
        // Remove users from this blog.
        if (!empty($users)) {
            foreach ($users as $user_id) {
                remove_user_from_blog($user_id, $blog_id);
            }
        }
        update_blog_status($blog_id, 'deleted', 1);
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('deleted_blog', array($blog_id, false), '5.1.0');
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 6.2

/**
 * Deletes a site.
 *
 * @since 3.0.0
 * @since 5.1.0 Use wp_delete_site() internally to delete the site row from the database.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $blog_id = (int) $blog_id;
    $switch = false;
    if (get_current_blog_id() !== $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 === $blog_id || is_main_site($blog_id) || $blog->path === $current_network->path && $blog->domain === $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        wp_delete_site($blog_id);
    } else {
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('delete_blog', array($blog_id, false), '5.1.0');
        $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
        // Remove users from this blog.
        if (!empty($users)) {
            foreach ($users as $user_id) {
                remove_user_from_blog($user_id, $blog_id);
            }
        }
        update_blog_status($blog_id, 'deleted', 1);
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('deleted_blog', array($blog_id, false), '5.1.0');
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 5.9

/**
 * Delete a site.
 *
 * @since 3.0.0
 * @since 5.1.0 Use wp_delete_site() internally to delete the site row from the database.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $blog_id = (int) $blog_id;
    $switch = false;
    if (get_current_blog_id() !== $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 === $blog_id || is_main_site($blog_id) || $blog->path === $current_network->path && $blog->domain === $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        wp_delete_site($blog_id);
    } else {
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('delete_blog', array($blog_id, false), '5.1.0');
        $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
        // Remove users from this blog.
        if (!empty($users)) {
            foreach ($users as $user_id) {
                remove_user_from_blog($user_id, $blog_id);
            }
        }
        update_blog_status($blog_id, 'deleted', 1);
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('deleted_blog', array($blog_id, false), '5.1.0');
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 5.5

/**
 * Delete a site.
 *
 * @since 3.0.0
 * @since 5.1.0 Use wp_delete_site() internally to delete the site row from the database.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_network->path && $blog->domain == $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        wp_delete_site($blog_id);
    } else {
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('delete_blog', array($blog_id, false), '5.1.0');
        $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
        // Remove users from this blog.
        if (!empty($users)) {
            foreach ($users as $user_id) {
                remove_user_from_blog($user_id, $blog_id);
            }
        }
        update_blog_status($blog_id, 'deleted', 1);
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('deleted_blog', array($blog_id, false), '5.1.0');
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 5.1

/**
 * Delete a site.
 *
 * @since 3.0.0
 * @since 5.1.0 Use wp_delete_site() internally to delete the site row from the database.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_network->path && $blog->domain == $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        wp_delete_site($blog_id);
    } else {
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('delete_blog', array($blog_id, false), '5.1.0');
        $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
        // Remove users from this blog.
        if (!empty($users)) {
            foreach ($users as $user_id) {
                remove_user_from_blog($user_id, $blog_id);
            }
        }
        update_blog_status($blog_id, 'deleted', 1);
        /** This action is documented in wp-includes/ms-blogs.php */
        do_action_deprecated('deleted_blog', array($blog_id, false), '5.1.0');
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.9

/**
 * Delete a site.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    /**
     * Fires before a site is deleted.
     *
     * @since MU (3.0.0)
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_network->path && $blog->domain == $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $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 array $tables  The site tables to be dropped.
         * @param int   $blog_id The ID of the site to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filters the upload base directory to delete when the site is deleted.
         *
         * @since MU (3.0.0)
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The site ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    /**
     * Fires after the site is deleted from the network.
     *
     * @since 4.8.0
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's tables should be dropped. Default is false.
     */
    do_action('deleted_blog', $blog_id, $drop);
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.8

/**
 * Delete a site.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    /**
     * Fires before a site is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_network->path && $blog->domain == $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_get_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filters the tables to drop when the site is deleted.
         *
         * @since MU
         *
         * @param array $tables  The site tables to be dropped.
         * @param int   $blog_id The ID of the site to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filters the upload base directory to delete when the site is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The site ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    /**
     * Fires after the site is deleted from the network.
     *
     * @since 4.8.0
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's tables should be dropped. Default is false.
     */
    do_action('deleted_blog', $blog_id, $drop);
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.7

/**
 * Delete a site.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_site($blog_id);
    /**
     * Fires before a site is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_network = get_network();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_network->path && $blog->domain == $current_network->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_get_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filters the tables to drop when the site is deleted.
         *
         * @since MU
         *
         * @param array $tables  The site tables to be dropped.
         * @param int   $blog_id The ID of the site to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filters the upload base directory to delete when the site is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The site ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.6

/**
 * Delete a site.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a site is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_get_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filters the tables to drop when the site is deleted.
         *
         * @since MU
         *
         * @param array $tables  The site tables to be dropped.
         * @param int   $blog_id The ID of the site to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filters the upload base directory to delete when the site is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The site ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.5

/**
 * Delete a site.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Site ID.
 * @param bool $drop    True if site's database tables should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a site is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The site ID.
     * @param bool $drop    True if site's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_get_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the site is deleted.
         *
         * @since MU
         *
         * @param array $tables  The site tables to be dropped.
         * @param int   $blog_id The ID of the site to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filter the upload base directory to delete when the site is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The site ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.4

/**
 * Delete a blog.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int  $blog_id Blog ID.
 * @param bool $drop    True if blog's table should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a blog is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The blog ID.
     * @param bool $drop    True if blog's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the blog is deleted.
         *
         * @since MU
         *
         * @param array $tables  The blog tables to be dropped.
         * @param int   $blog_id The ID of the blog to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filter the upload base directory to delete when the blog is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The blog ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.3

/**
 * Delete a blog.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb
 *
 * @param int  $blog_id Blog ID.
 * @param bool $drop    True if blog's table should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a blog is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The blog ID.
     * @param bool $drop    True if blog's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the blog is deleted.
         *
         * @since MU
         *
         * @param array $tables  The blog tables to be dropped.
         * @param int   $blog_id The ID of the blog to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filter the upload base directory to delete when the blog is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The blog ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.2

/**
 * Delete a blog.
 *
 * @since 3.0.0
 *
 * @param int  $blog_id Blog ID.
 * @param bool $drop    True if blog's table should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a blog is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The blog ID.
     * @param bool $drop    True if blog's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the blog is deleted.
         *
         * @since MU
         *
         * @param array $tables  The blog tables to be dropped.
         * @param int   $blog_id The ID of the blog to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filter the upload base directory to delete when the blog is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The blog ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        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);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 4.1

/**
 * Delete a blog.
 *
 * @since 3.0.0
 *
 * @param int  $blog_id Blog ID.
 * @param bool $drop    True if blog's table should be dropped. Default is false.
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a blog is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The blog ID.
     * @param bool $drop    True if blog's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // If a full blog object is not available, do not destroy anything.
    if ($drop && !$blog) {
        $drop = false;
    }
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    $upload_path = trim(get_option('upload_path'));
    // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
    if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) {
        $drop = false;
    }
    if ($drop) {
        $uploads = wp_upload_dir();
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the blog is deleted.
         *
         * @since MU
         *
         * @param array $tables  The blog tables to be dropped.
         * @param int   $blog_id The ID of the blog to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        /**
         * Filter the upload base directory to delete when the blog is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The blog ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        continue;
                    }
                    if (@is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
                        $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
                    } else if (@is_file($dir . DIRECTORY_SEPARATOR . $file)) {
                        @unlink($dir . DIRECTORY_SEPARATOR . $file);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 3.8

/**
 * Delete a blog
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param bool $drop True if blog's table should be dropped. Default is false.
 * @return void
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a blog is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The blog ID.
     * @param bool $drop    True if blog's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    $current_site = get_current_site();
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    if ($drop) {
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the blog is deleted.
         *
         * @since MU
         *
         * @param array $tables  The blog tables to be dropped.
         * @param int   $blog_id The ID of the blog to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        $uploads = wp_upload_dir();
        /**
         * Filter the upload base directory to delete when the blog is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The blog ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        continue;
                    }
                    if (@is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
                        $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
                    } else if (@is_file($dir . DIRECTORY_SEPARATOR . $file)) {
                        @unlink($dir . DIRECTORY_SEPARATOR . $file);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}

WordPress Version: 3.7

/**
 * Delete a blog
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param bool $drop True if blog's table should be dropped. Default is false.
 * @return void
 */
function wpmu_delete_blog($blog_id, $drop = false)
{
    global $wpdb, $current_site;
    $switch = false;
    if (get_current_blog_id() != $blog_id) {
        $switch = true;
        switch_to_blog($blog_id);
    }
    $blog = get_blog_details($blog_id);
    /**
     * Fires before a blog is deleted.
     *
     * @since MU
     *
     * @param int  $blog_id The blog ID.
     * @param bool $drop    True if blog's table should be dropped. Default is false.
     */
    do_action('delete_blog', $blog_id, $drop);
    $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids'));
    // Remove users from this blog.
    if (!empty($users)) {
        foreach ($users as $user_id) {
            remove_user_from_blog($user_id, $blog_id);
        }
    }
    update_blog_status($blog_id, 'deleted', 1);
    // Don't destroy the initial, main, or root blog.
    if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) {
        $drop = false;
    }
    if ($drop) {
        $tables = $wpdb->tables('blog');
        /**
         * Filter the tables to drop when the blog is deleted.
         *
         * @since MU
         *
         * @param array $tables  The blog tables to be dropped.
         * @param int   $blog_id The ID of the blog to drop tables for.
         */
        $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id);
        foreach ((array) $drop_tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `{$table}`");
        }
        $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id));
        $uploads = wp_upload_dir();
        /**
         * Filter the upload base directory to delete when the blog is deleted.
         *
         * @since MU
         *
         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
         * @param int    $blog_id            The blog ID.
         */
        $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
            $dh = @opendir($dir);
            if ($dh) {
                while (($file = @readdir($dh)) !== false) {
                    if ($file == '.' || $file == '..') {
                        continue;
                    }
                    if (@is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
                        $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
                    } else if (@is_file($dir . DIRECTORY_SEPARATOR . $file)) {
                        @unlink($dir . DIRECTORY_SEPARATOR . $file);
                    }
                }
                @closedir($dh);
            }
            $index++;
        }
        $stack = array_reverse($stack);
        // Last added dirs are deepest
        foreach ((array) $stack as $dir) {
            if ($dir != $top_dir) {
                @rmdir($dir);
            }
        }
        clean_blog_cache($blog);
    }
    if ($switch) {
        restore_current_blog();
    }
}