Drupal 8: delete all nodes of the same type

megastruktur picture megastruktur · Jan 4, 2016 · Viewed 16.8k times · Source

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!

Answer

colan picture colan · Jul 7, 2016

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.