Flag delete data when session expire

Created on 8 April 2015, over 10 years ago
Updated 19 August 2025, 6 days ago

If you use flag with session api , when the session expire the flag is deleted and all the associated data (fields).

This is a huge problem because you are deleting data. Specially if field are associated with the flag.

We lost thousands of thousands of records without knowing it. Now I will have to go throw all the backup and "merge" them...

The problem is with this hook on flag.module

/**
 * Implements hook_session_api_cleanup().
 *
 * Clear out anonymous user flaggings during Session API cleanup.
 */
function flag_session_api_cleanup($arg = 'run') {
  // Session API 1.1 version:
  if ($arg == 'run') {
    $query = db_select('flagging', 'fc');
    $query->leftJoin('session_api', 's', 'fc.sid = s.sid');
    $result = $query
      ->fields('fc', array('sid'))
      ->condition('fc.sid', 0, '<>')
      ->isNull('s.sid')
      ->execute();
    foreach ($result as $row) {
      db_delete('flagging')
        ->condition('sid', $row->sid)
        ->execute();
    }
  }
  // Session API 1.2+ version.
  elseif (is_array($arg)) {
    $outdated_sids = $arg;
    db_delete('flagging')->condition('sid', $outdated_sids, 'IN')->execute();
  }
}
🐛 Bug report
Status

Closed: outdated

Version

3.0

Component

Flag core

Created by

🇨🇭Switzerland gagarine

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024