7

Revisions and how to delete them all. For Drupal 7, there’re plenty of modules that do that or even limit the revisions that are kept or even delete everything that is older then x-days, but not for Drupal 8.

It seems there is nothing out there. Searching through the database is something I like not to do.

Any ideas?

8

Yes, as you says there is a list of modules that can do this in Drupal 7 like:

Node Revision Delete

The Node Revision Delete module lets you to track and prune old revisions of content types. Define the max amount of revisions to keep per content type. Run on Drush, cron run or on a different period (daily, weekly, etc).

Node Revision Restrict

The Node Revision Restrict module allows you to restrict revisions of node for each content type. This module has an admin interface to specify node revision threshold for each content type available. The possible setting include node type with corresponding revision limit, provided on the Node Revision Restrict configuration form. Note that this module does not and cannot delete the current revisions of a node.

Node Revision Cleanup

Node Revision Cleanup is designed for really big databases. And site administrators who are comfortable with Drush. The concept behind this module is to be able to run multiple invocations at the same time, via Drush to maximize the number of node revision deletions per second.

Revision Deletion

This modules enables the ability to mass delete aging node revisions. Possible settings include node type, the age of node revision before being deleted, along with a Cron frequency setting. The revisions may be deleted after review on an administer page as well. This module will NEVER delete the current revision of a node, nor will it allow you to do so.

And for Drupal 8, well at this moment there is not module for do that, but you can use:

To get the revision list of a node:

$vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);

See Nodes now have a storage controller; node_revision_list() removed.

To delete a revision revision you can use the node_revision_delete() function, but at the end see the function code:

function node_revision_delete($revision_id) {
  \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($revision_id);
}

So, is better if you just use:

\Drupal::entityTypeManager()->getStorage('node')->deleteRevision($revision_id);

  • For getting the revision list, is there an alternative for entityManager? I think its deprecated. – Kimberly W Jul 8 '18 at 22:16
  • 1
    @KimberlyW Just change entityTypeManager() for entityManager() and should works. – Adrian Cid Almaguer Jul 9 '18 at 15:54
  • 1
    You can find content revision while creating a new view. And you can have option for delete revision link there. – Vishal Kumar Sahu Aug 1 '18 at 18:53
  • Same for this answer: entityTypeManager()->getStorage() return a EntityStorageInterface. It does not have "revisionIds" method. You must explicitly inject NodeStorageInterface to have the method. – ssibal Dec 17 '18 at 15:34
  • @ssibal just read: api.drupal.org/api/drupal/… – Adrian Cid Almaguer Dec 17 '18 at 16:33
2

Snippet to delete all but the latest revision of a specific node:

<?php
  try {
    $nid = 123; // Your node id
    $vids = \Drupal::entityManager()->getStorage('node')->revisionIds(node_load($nid));
    foreach($vids as $vid){        
      \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($vid);
    }
  } catch (Exception $e) {
    // Latest revision will not be deleted but throws an error
    return $e->getMessage();
  }
?>
  • 1
    brilliant Thomas. I found this a better option seeing that none of the modules in Adrian's answer actually work in D8. Yours actually works when added to MYTHEME.theme file. Could you maybe add a loop to the start so that it runs through all of the nodes? That would make this super for taking a dev site to production and dumping all the dev revisions to lean up and clean up. – piet May 29 '18 at 22:26
2

The suggestions in here are correct, though a few things is worth mention.

$vids = \Drupal::entityManager()->getStorage('node')->revisionIds(node_load($nid));

But since this is marked as deprecated, it should be replaced by:

$vids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);

The example code in here also try delete the default revision and that will cause [error] Default revision can not be deleted. To avoid that I suggest that you wrap the removal in something like:

  // If revision id is not default, remove it.
  if ($vid !== $node->getLoadedRevisionId()) {
    print 'Removing revision ' . $vid . PHP_EOL;
    \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($vid);
  }
  • This is a completely false answer. entityTypeManager()->getStorage() return a EntityStorageInterface. It does not have "revisionIds" method. – ssibal Dec 17 '18 at 15:32
  • @ssibal true, but since we're dealing with node storage, we will be getting NodeStorageInterface, which does. – akalata Jul 15 at 19:26
2

There is a working module for Drupal 8: Node Revisions Autoclean.

This module allows to manage node's revisions store : according to admin's settings, older revisions are automatically deleted.

You may choose how many revisions you want to keep for each node type. Latest drafts coming after a published revision are never deleted. You may also choose to delete revisions by date.

For larger websites with many node, you may choose to delete revision by cron or a drush command (drush 9 only).

Do not expect it to run on normal cron runs, yet. But it can be set to run on updating nodes. Here is the issue for the cron, which I do not fully understand: https://www.drupal.org/project/node_revisions_autoclean/issues/2958655

1

Please inject always a storage in the proper way:

  # my_module.services.yml
  node_storage: 
    class: Drupal\node\NodeStorage
    factory: entity_type.manager:getStorage
    arguments: ['node']
  my_service:
    class: Drupal\my_module\MyService
    arguments: ['@node_storage']

In this case you can already have in your class constructor the following:

class MyService {

  /**
   * @var \Drupal\node\NodeStorageInterface
   */
  private $storage;

  public function __construct(NodeStorageInterface $storage) {
    $this->storage = $storage;
  }

}

In this case your IDE will also easily list all of the available methods unlike with this solution:

\Drupal::entityManager()->getStorage('node')->revisionIds(node_load($nid));

Remember EntityTypeManager is in most of the case "too much" power: use only as much as you need but not more. Service configurations can help you with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.