diff --git a/spam.install b/spam.install
index d184093..421fa6c 100644
--- a/spam.install
+++ b/spam.install
@@ -181,3 +181,63 @@ function spam_update_6003() {
   return $ret;
 }
 
+/**
+ * Update the comment counts on existing nodes.
+ */
+function spam_update_6004() {
+  $batch = array(
+    'title' => t('Updating node comment counts'),
+    'operations' => array(
+      array('_spam_update_6004_update_comment_counts', array()),
+    ),
+    'file' => drupal_get_path('module', 'spam') .'/spam.install',
+    'finished' => '_spam_update_6004_update_comment_counts_finished',
+  );
+  batch_set($batch);
+  return array();
+}
+
+/**
+ * Batch function to update the comment counts of nodes.
+ */
+function _spam_update_6004_update_comment_counts($context) {
+  if (empty($context['sandbox'])) {
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['current_node'] = 0;
+    $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}'));
+  }
+  $limit = 5;
+  $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
+  while ($row = db_fetch_array($result)) {
+    $count = db_result(db_query("SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d", $row['nid']));
+    _comment_update_node_statistics($row['nid']);
+    if($count != db_result(db_query("SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d", $row['nid']))){
+      $node = node_load($row['nid']);
+      $context['results'][] =  $context['message'] =  t('The count was changed from %count to %new at %title (nid: %nid).', array('%title' => $node->title, '%nid' => $node->nid, '%count' => $count, '%new' => $node->comment_count));
+    }
+    $context['sandbox']['progress']++;
+    $context['sandbox']['current_node'] = $row['nid'];
+  }
+  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
+    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+  }
+}
+
+/**
+* Batch 'finished' callback
+*/
+function _spam_update_6004_update_comment_counts_finished($success, $results, $operations) {
+  if ($success) {
+    // Here we do something meaningful with the results.
+    $message = count($results) .' corrected comment counts.';
+    $message .= theme('item_list', $results);  // D6 syntax
+    drupal_set_message($message);
+  }
+  else {
+    // An error occurred.
+    // $operations contains the operations that remained unprocessed.
+    $error_operation = reset($operations);
+    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
+    drupal_set_message($message, 'error');
+  }
+}
