Skip to content

Instantly share code, notes, and snippets.

@btmash
Last active June 22, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btmash/95265a63e510374b0562a140395c78cf to your computer and use it in GitHub Desktop.
Save btmash/95265a63e510374b0562a140395c78cf to your computer and use it in GitHub Desktop.
Delete Research nodes revisions
<?php
// Query all of the nids of the research content type.
$nids = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', 'research', '=')
->execute()
->fetchCol();
foreach ($nids as $nid) {
// The task now is to delete all revisions except the most recent 20 belonging to each of the nodes in the $nodes array.
// We probably need a loop here that can loops through the $nodes array and then run the following on each node in the array
// As written, the code below would delete all revisions belonging to each node, we need to limit that to those after the most recent 20.
// Possible way to limit the array of revisons to delete: $revstodelete = array_slice($vid, < 19); to return revisions after the first 20.
$node = node_load($nid, TRUE);
$vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
$vids = array_slice($vids, 0, -20);
foreach($vids as $vid){
if ($node->vid !== $vid) {
\Drupal::entityTypeManager()->getStorage('node')->deleteRevision($vid);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment