@@ -288,6 +288,9 @@ public function empty_( $args, $assoc_args ) {
288288 * [--keep-tables]
289289 * : Delete the blog from the list, but don't drop its tables.
290290 *
291+ * [--delete-tables-with-prefix]
292+ * : Delete all tables with the site's database table prefix after deleting the site.
293+ *
291294 * ## EXAMPLES
292295 *
293296 * $ wp site delete 123
@@ -299,6 +302,10 @@ public function delete( $args, $assoc_args ) {
299302 WP_CLI ::error ( 'This is not a multisite installation. ' );
300303 }
301304
305+ if ( Utils \get_flag_value ( $ assoc_args , 'keep-tables ' ) && Utils \get_flag_value ( $ assoc_args , 'delete-tables-with-prefix ' ) ) {
306+ WP_CLI ::error ( "The '--keep-tables' and '--delete-tables-with-prefix' flags cannot be used together. " );
307+ }
308+
302309 if ( isset ( $ assoc_args ['slug ' ] ) ) {
303310 $ blog_id = get_id_from_blogname ( $ assoc_args ['slug ' ] );
304311 if ( null === $ blog_id ) {
@@ -329,9 +336,39 @@ public function delete( $args, $assoc_args ) {
329336
330337 wpmu_delete_blog ( (int ) $ blog ->blog_id , ! Utils \get_flag_value ( $ assoc_args , 'keep-tables ' ) );
331338
339+ if ( Utils \get_flag_value ( $ assoc_args , 'delete-tables-with-prefix ' ) ) {
340+ $ this ->drop_tables_with_prefix ( (int ) $ blog ->blog_id );
341+ }
342+
332343 WP_CLI ::success ( "The site at ' {$ site_url }' was deleted. " );
333344 }
334345
346+ /**
347+ * Drops all database tables for a site prefix.
348+ *
349+ * @param int $blog_id Site ID.
350+ */
351+ private function drop_tables_with_prefix ( $ blog_id ) {
352+ global $ wpdb ;
353+
354+ $ prefix_like = $ wpdb ->esc_like ( $ wpdb ->get_blog_prefix ( $ blog_id ) ) . '% ' ;
355+ $ tables = $ wpdb ->get_col ( $ wpdb ->prepare ( 'SHOW TABLES LIKE %s ' , $ prefix_like ) );
356+
357+ if ( empty ( $ tables ) ) {
358+ return ;
359+ }
360+
361+ $ tables = array_map (
362+ static function ( $ table ) {
363+ return '` ' . str_replace ( '` ' , '`` ' , $ table ) . '` ' ;
364+ },
365+ $ tables
366+ );
367+
368+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table identifiers are escaped and cannot be passed as placeholders.
369+ $ wpdb ->query ( 'DROP TABLE IF EXISTS ' . implode ( ', ' , $ tables ) );
370+ }
371+
335372 /**
336373 * Gets details about a site in a multisite installation.
337374 *
0 commit comments