Skip to content

Add --delete-tables-with-prefix to wp site delete for multisite custom table cleanup#617

Draft
Copilot wants to merge 4 commits into
mainfrom
copilot/wp-site-delete-remove-custom-tables
Draft

Add --delete-tables-with-prefix to wp site delete for multisite custom table cleanup#617
Copilot wants to merge 4 commits into
mainfrom
copilot/wp-site-delete-remove-custom-tables

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 27, 2026

wp site delete currently delegates to core deletion APIs, which can leave plugin-created custom tables behind unless plugins register them via core hooks. This PR adds an explicit, opt-in cleanup path to drop remaining tables by site DB prefix.

  • CLI behavior: opt-in prefix cleanup

    • Adds --delete-tables-with-prefix to wp site delete.
    • After site deletion, the command finds tables matching the deleted site prefix ($wpdb->get_blog_prefix( $blog_id ) . '%') and drops them.
  • Safety and flag semantics

    • Adds explicit validation so --keep-tables and --delete-tables-with-prefix cannot be used together.
    • Keeps default behavior unchanged unless the new flag is passed.
  • Coverage for new behavior

    • Adds feature scenarios for:
      • Deleting a site with --delete-tables-with-prefix and verifying prefixed custom tables are removed.
      • Rejecting invalid combined usage with --keep-tables.
wp site delete <site-id> --yes --delete-tables-with-prefix

Copilot AI linked an issue May 27, 2026 that may be closed by this pull request
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add option to remove custom tables on wp site delete Add --delete-tables-with-prefix to wp site delete for multisite custom table cleanup May 27, 2026
Copilot AI requested a review from swissspidy May 27, 2026 09:47
@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

❌ Patch coverage is 79.16667% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Site_Command.php 79.16% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread features/site.feature Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the multisite wp site delete command with an opt-in --delete-tables-with-prefix flag to drop any remaining custom tables that match the deleted site’s DB table prefix, and adds Behat coverage for the new flag and invalid flag combinations.

Changes:

  • Added --delete-tables-with-prefix flag to wp site delete and implemented prefix-based table dropping.
  • Added validation to prevent combining --keep-tables with --delete-tables-with-prefix.
  • Added Behat scenarios to verify table cleanup and flag incompatibility.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Site_Command.php Adds new CLI flag, validates flags, and implements table drop-by-prefix behavior.
features/site.feature Adds Behat scenarios covering the new cleanup flag and invalid flag combination.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Site_Command.php
Comment on lines +305 to +307
if ( Utils\get_flag_value( $assoc_args, 'keep-tables' ) && Utils\get_flag_value( $assoc_args, 'delete-tables-with-prefix' ) ) {
WP_CLI::error( "The '--keep-tables' and '--delete-tables-with-prefix' flags cannot be used together." );
}
Comment thread src/Site_Command.php Outdated
Comment on lines 337 to 338
wpmu_delete_blog( (int) $blog->blog_id, ! Utils\get_flag_value( $assoc_args, 'keep-tables' ) );

Comment thread src/Site_Command.php Outdated
Comment on lines +354 to +369
$prefix_like = $wpdb->esc_like( $wpdb->get_blog_prefix( $blog_id ) ) . '%';
$tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $prefix_like ) );

if ( empty( $tables ) ) {
return;
}

$tables = array_map(
static function ( $table ) {
return '`' . str_replace( '`', '``', $table ) . '`';
},
$tables
);

// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table identifiers are escaped and cannot be passed as placeholders.
$wpdb->query( 'DROP TABLE IF EXISTS ' . implode( ', ', $tables ) );
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI requested a review from swissspidy May 27, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp site delete leaves custom tables in DB

3 participants