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,26 @@
 	$pps = bbp_get_public_status_id();
 	$cps = bbp_get_closed_status_id();
 
+	// Get an array of topic id's and delete the meta key _bbp_reply_count for each
+	$topics  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '{$tpt}';", OBJECT_K );
+
+	if ( !empty( $topics ) ) {
+		$topic_ids = array();
+		foreach ( $topics as $key => $value ) {
+			$topic_ids[] = $key;
+		}
+
+		$topic_ids   = implode( ", ", $topic_ids );
+		$sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `post_id` IN ({$topic_ids}) AND `meta_key` = '_bbp_reply_count';";
+		if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
+			return array( 1, sprintf( $statement, $result ) );
+		}
+	}
+
+	// Cleanup
+	unset( $topics, $topic_ids, $sql_delete );
+
+	// 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 +362,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 +636,29 @@
 	$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 ) ) )
-		return array( 1, sprintf( $statement, $result ) );
+	// Post types and status
+	$fpt = bbp_get_forum_post_type();
 
+	// Get an array of forum id's and delete the meta key _bbp_reply_count for each
+	$forums  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '{$fpt}';", OBJECT_K );
+
+	if ( !empty( $forums ) ) {
+		$forums_ids = array();
+		foreach ( $forums as $key => $value ) {
+			$forums_ids[] = $key;
+		}
+
+		$forums_ids   = implode( ", ", $forums_ids );
+		$sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `post_id` IN ({$forums_ids}) AND `meta_key` = '_bbp_reply_count';";
+		if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
+			return array( 1, sprintf( $statement, $result ) );
+		}
+	}
+
+	// Cleanup
+	unset( $forums, $forums_ids, $sql_delete );
+
+	// Recalculate the meta key _bbp_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 +1520,4 @@
 			bbp_admin_tools_feedback( $message );
 		}
 	}
-}
\ No newline at end of file
+}
