Outdated revisions are not added to queue unless they are resaved

Created on 12 March 2025, 5 months ago

Problem/Motivation

I read thru the code a noticed that there are no cron tasks to add outdated revisions to the queue.
The queue adding relies on the node_update hook to check if the conditions are met, but if the nodes are not saved then they are not added to the queue.

Steps to reproduce

  • Set option "Delete revisions after a specific amount of time." enabled
  • Set the min time 1 month and save
  • Wait for 1 month without saving your nodes with outdated revisions
  • Your revision are undeleted after the month

Proposed resolution

This type of query with releted configs should be written for all content types and added to the queue.

/**
 * Implements hook_cron().
 *
 * Add node revisions older than 1 months and have more than 4 revisions.
 * to the Node Revision Delete queue.
 */
function node_revision_delete_cron() {
  if (\Drupal::moduleHandler()->moduleExists('node_revision_delete')) {
    $db = \Drupal::database();
    $query = $db->select('node_revision', 'nr')
      ->fields('nr', ['nid'])
      ->condition('nr.revision_timestamp', strtotime('-1 months'), '<')
      ->groupBy('nr.nid')
      ->having('COUNT(nr.vid) > :revision_count', [':revision_count' => 4]);
    $results = $query->execute();
    $nids = $results->fetchCol();
    if (!empty($nids)) {
      $nodeRevisionDelete = \Drupal::service('node_revision_delete');
      $nids_in_queue = $nodeRevisionDelete->getNidsInQueue();
      foreach ($nids as $nid) {
        if (($nid = (int) $nid) && !isset($nids_in_queue[$nid])) {
          \Drupal::queue('node_revision_delete')->createItem($nid);
          if (\Drupal::config('node_revision_delete.settings')->get('verbose_log')) {
            \Drupal::logger('node_revision_delete')->info('Node @id was added to the Node Revision Delete queue.', ['@id' => $nid]);
          }
        }
      }
    }
  }
}

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇫🇮Finland onnia

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

Comments & Activities

  • Issue created by @onnia
  • 🇨🇦Canada bdunphy

    Can confirm and I have bumped against this bug as well. This makes the module less useful in production uses. Having to save the outdated node in order to queue up revision deletion is not practical.

Production build 0.71.5 2024