Created
April 16, 2020 06:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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