Skip to content

Instantly share code, notes, and snippets.

@d0t15t
Created April 16, 2020 06:14
Show Gist options
  • Save d0t15t/743355644463ee74039002a0585d60f0 to your computer and use it in GitHub Desktop.
Save d0t15t/743355644463ee74039002a0585d60f0 to your computer and use it in GitHub Desktop.
For Testing: Delete most nodes, leave a limited, randomly sorted of each content type.
/**
* Implements hook_update_N().
*
* Remove nodes for faster testing.
*/
function hook_update_8001(&$sandbox) {
$etm = \Drupal::entityTypeManager();
$types = $etm->getStorage('node_type')->loadMultiple();
$nodeStorage = $etm->getStorage('node');
foreach ($types as $label => $type) {
$ids = $etm->getStorage('node')->getQuery()
->condition('type', $label)
->execute();
shuffle($ids);
$keep = array_slice($ids, 0, 25);
$remove = array_diff($ids, $keep);
foreach($remove as $id) {
$node = $nodeStorage->load($id);
if ($nodeStorage->delete([$node])) {
\Drupal::messenger()->addMessage((int) $id . ' Deleted');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment