Index: src/includes/admin/tools.php
===================================================================
--- src/includes/admin/tools.php	(revision 5341)
+++ src/includes/admin/tools.php	(working copy)
@@ -325,10 +325,6 @@
 	$statement = __( 'Counting the number of replies in each topic&hellip; %s', 'bbpress' );
 	$result    = __( 'Failed!', 'bbpress' );
 
-	$sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_count';";
-	if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
-		return array( 1, sprintf( $statement, $result ) );
-
 	// Post types and status
 	$tpt = bbp_get_topic_post_type();
 	$rpt = bbp_get_reply_post_type();
@@ -335,6 +331,16 @@
 	$pps = bbp_get_public_status_id();
 	$cps = bbp_get_closed_status_id();
 
+	// Delete the meta key _bbp_reply_count for each topic
+	$sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
+						LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
+						WHERE `posts`.`post_type` = '{$tpt}'
+						AND `postmeta`.`meta_key` = '_bbp_reply_count'";
+	if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
+		return array( 1, sprintf( $statement, $result ) );
+	}
+
+	// Recalculate the meta key _bbp_reply_count for each topic
 	$sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
 			SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value`
 				FROM `{$wpdb->posts}` AS `topics`
@@ -346,8 +352,9 @@
 					AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
 				GROUP BY `topics`.`ID`);";
 
-	if ( is_wp_error( $wpdb->query( $sql ) ) )
+	if ( is_wp_error( $wpdb->query( $sql ) ) ) {
 		return array( 2, sprintf( $statement, $result ) );
+	}
 
 	return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
 }
@@ -619,10 +626,21 @@
 	$statement = __( 'Counting the number of replies in each forum&hellip; %s', 'bbpress' );
 	$result    = __( 'Failed!', 'bbpress' );
 
-	$sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_reply_count', '_bbp_total_reply_count' );";
-	if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
+	// Post types and status
+	$fpt = bbp_get_forum_post_type();
+
+	// Delete the meta keys _bbp_reply_count and _bbp_total_reply_count for each forum
+	$sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
+						LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
+						WHERE `posts`.`post_type` = '{$fpt}'
+						AND `postmeta`.`meta_key` = '_bbp_reply_count'
+						OR `postmeta`.`meta_key` = '_bbp_total_reply_count'";
+
+	if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
 		return array( 1, sprintf( $statement, $result ) );
+	}
 
+	// Recalculate the metas key _bbp_reply_count and _bbp_total_reply_count for each forum
 	$forums = get_posts( array( 'post_type' => bbp_get_forum_post_type(), 'numberposts' => -1 ) );
 	if ( !empty( $forums ) ) {
 		foreach ( $forums as $forum ) {
@@ -1484,4 +1502,4 @@
 			bbp_admin_tools_feedback( $message );
 		}
 	}
-}
\ No newline at end of file
+}
