diff --git a/flag.inc b/flag.inc
index d11ddf7..3353154 100644
--- a/flag.inc
+++ b/flag.inc
@@ -194,6 +194,12 @@ class flag_flag {
         'unflag' => array(DRUPAL_AUTHENTICATED_RID),
       ),
       'weight' => 0,
+
+      // If this option is set to TRUE, any anonymous flags created in a
+      // session that has since expired will be deleted from the flag_content
+      // table in flag_session_api_cleanup(). The data in flag_counts will not
+      // be affected.
+      'delete_expired_flag_content' => TRUE,
     );
 
     // Merge in options from the current link type.
diff --git a/flag.module b/flag.module
index ec4a5b1..e4a235d 100644
--- a/flag.module
+++ b/flag.module
@@ -566,17 +566,37 @@ function flag_user($op, &$edit, &$account, $category = NULL) {
  * Clear out anonymous user flaggings during Session API cleanup.
  */
 function flag_session_api_cleanup($arg = 'run') {
+  // Get all flags which should be truncated.
+  $flags = flag_get_flags();
+  $fids = array();
+  foreach ($flags as $flag) {
+    if (!empty($flag->delete_expired_flag_content)) {
+      $fids[] = $flag->fid;
+    }
+  }
+
+  // Flags are configured to clean-up records by default, but if disabled on all
+  // flags, no need to do any work.
+  if (empty($fids)) {
+    return;
+  }
+
   // Session API 1.1 version:
   if ($arg == 'run') {
     $result = db_query("SELECT fc.sid FROM {flag_content} fc LEFT JOIN {session_api} s ON (fc.sid = s.sid) WHERE fc.sid <> 0 AND s.sid IS NULL");
+    $arguments = array_merge(array(0), $fids);
+    $db_placeholders = db_placeholders($fids);
     while ($row = db_fetch_object($result)) {
-      db_query("DELETE FROM {flag_content} WHERE sid = %d", $row->sid);
+      db_query("DELETE FROM {flag_content} WHERE sid = %d AND fid IN ($db_placeholders)", $arguments);
     }
   }
   // Session API 1.2+ version.
   elseif (is_array($arg)) {
     $outdated_sids = $arg;
-    db_query('DELETE FROM {flag_content} WHERE sid IN (' . implode(',', $outdated_sids) . ')');
+    $arguments = array_merge($outdated_sids, $fids);
+    $sid_placeholders = db_placeholders($outdated_sids);
+    $fid_placeholders = db_placeholders($fids);
+    db_query('DELETE FROM {flag_content} WHERE sid IN ($sid_placeholders) AND fid IN ($fid_placeholders)', $arguments);
   }
 }
 
