How to Delete Old Revisions for Each Content Type in Drupal 9?

Neeraj Singh
2 min readJun 16, 2024

--

Delete old Drupal revisions

Managing content revisions in Drupal can be a challenge, especially with frequent updates leading to a bloated database. While having a revision history is beneficial, not all past revisions are necessary, particularly if you have regular database backups. Reducing the number of stored revisions can significantly improve your Drupal site’s performance.

In this guide, we’ll walk through creating a custom Drupal script to delete older revisions for all content types, keeping only the most recent ones automatically.

Deleting Older Revisions for All Content Types

The following PHP script will help you manage database bloat and enhance site performance by removing unnecessary revisions. This script is designed for Drupal sites and ensures that only the latest two revisions are kept for each content type.

Important Note

Before executing this script, backup your database. Deleting revisions is irreversible, and having a backup ensures you can restore your data if needed.

Script

<?php
// Set memory limit to 4000M and execution time to unlimited
ini_set('memory_limit', '4000M');
set_time_limit(0);
ini_set('max_execution_time', 0);

use Drupal\Core\Entity\EntityTypeManagerInterface;

// Define a function to delete older revisions for all content types.
function delete_older_revisions_for_all_types(EntityTypeManagerInterface $entityTypeManager) {
$query = $entityTypeManager->getStorage('node')
->getQuery()
->sort('type', 'DESC');
$contentTypes = $query->execute();

// Loop through each content type.
foreach ($contentTypes as $contentType) {
$node = $entityTypeManager->getStorage('node')->load($contentType);
// Load all revisions of the current entity.
$revisionIds = $entityTypeManager->getStorage('node')->revisionIds($node);
// Sort revisions by revision ID in descending order.
krsort($revisionIds);
// Keep only the last 2 revisions.
$revisionsToKeep = array_slice($revisionIds, 0, 2);
// Delete older revisions.
foreach ($revisionIds as $revisionId) {
if (!in_array($revisionId, $revisionsToKeep)) {
$entityTypeManager->getStorage('node')->deleteRevision($revisionId);
echo PHP_EOL . "Revision ID: $revisionId deleted" . PHP_EOL;
}
}
}
}

// Call the function to delete older revisions for all content types.
delete_older_revisions_for_all_types(\Drupal::entityTypeManager());

Instructions

  • Set Memory and Execution Limits

Ensure your PHP configuration allows sufficient memory and execution time. This script sets the memory limit to 4000M or 4GB and removes the execution time limit.

  • Include Required Dependencies

This script utilizes the EntityTypeManagerInterface interface from the Drupal Core Entity module. Ensure that your Drupal installation includes this module.

  • Function Definition: delete_older_revisions_for_all_types

This function queries all content types, loads their nodes, and deletes older revisions, keeping only the latest two.

  • Execute the Script

Call the delete_older_revisions_for_all_types() function, passing an instance of the EntityTypeManagerInterface interface as an argument.

How to Execute the Script

  1. Save the script as node_revision_delete.php in your Drupal root folder.
  2. Run the script using Drush: ./drush php:script node_revision_delete.php

Final Advice

I strongly recommend testing this script in a UAT (User Acceptance Testing) environment first. Always ensure you have a backup of your database before executing the script in a production environment. If everything works as expected in UAT, proceed to production, but don’t forget to keep a backup.

--

--

Neeraj Singh

It’s been more than 12+ years in the Software Industry and really still looks like it’s deep and dark more than spark, I thought — So much still pending to know