I have a need to remove all nodes of the same type in Drupal 8 (there are over 7k of nodes).
It wouldn't be a problem for Drupal 7 (DB query + node_delete or node_delete_multiple would have solved my issue). However, D8 is slightly different :)
Please, advice, how can I do it. Thanks in advance!
Go to Administer -Content to see the list of all the nodes on your site. Filter the list to show just the one content_type. Select one node and delete it. This triggers your Rule and all the nodes are deleted.
If the node to be deleted is the head node, then simply point the head to the second node of the linked list. For all other nodes: Traverse the linked list and for the current node curr, check whether the next node contains the key that needs to be deleted.
One should use entity queries instead of acting directly on the database:
$result = \Drupal::entityQuery('node')
->condition('type', 'my_content_type_name')
->execute();
entity_delete_multiple('node', $result);
Setting up ranges like in the other answer shouldn't be too difficult.
See EntityFieldQuery has been rewritten for more information.
you can use the Devel module
see attached image for instructions.
attached image
The entity_delete_multiple is deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use the entity storage's delete() method to delete multiple entities:
// query all entities you want for example taxonomy term from tags vocabulary
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', 'tags');
$tids = $query->execute();
$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage_handler->loadMultiple($tids);
$storage_handler->delete($entities);
Well, the answer lies on the surface:
$types = array('my_content_type_name');
$nids_query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('n.type', $types, 'IN')
->range(0, 500)
->execute();
$nids = $nids_query->fetchCol();
entity_delete_multiple('node', $nids);
I advice you to use "range" and some sort of "batch" (or just re-run the code multiple times), because it's a very fat operation (500 nodes per operation is ok for 256MB).
To execute this code you can either write custom module OR use the devel module: https://www.drupal.org/project/devel
After installation go to yoursite_address/devel/php and execute php code there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With