Add --delete-tables-with-prefix to wp site delete for multisite custom table cleanup#617
Draft
Copilot wants to merge 4 commits into
Draft
Add --delete-tables-with-prefix to wp site delete for multisite custom table cleanup#617Copilot wants to merge 4 commits into
--delete-tables-with-prefix to wp site delete for multisite custom table cleanup#617Copilot wants to merge 4 commits into
Conversation
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 May 27, 2026
--delete-tables-with-prefix to wp site delete for multisite custom table cleanup
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
swissspidy
reviewed
May 27, 2026
Contributor
There was a problem hiding this comment.
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-prefixflag towp site deleteand implemented prefix-based table dropping. - Added validation to prevent combining
--keep-tableswith--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 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 on lines
337
to
338
| wpmu_delete_blog( (int) $blog->blog_id, ! Utils\get_flag_value( $assoc_args, 'keep-tables' ) ); | ||
|
|
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
wp site deletecurrently 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
--delete-tables-with-prefixtowp site delete.$wpdb->get_blog_prefix( $blog_id ) . '%') and drops them.Safety and flag semantics
--keep-tablesand--delete-tables-with-prefixcannot be used together.Coverage for new behavior
--delete-tables-with-prefixand verifying prefixed custom tables are removed.--keep-tables.