Delete multiple nodes in Drupal

Mr. Hoang ThienSeptember 25, 2022
  • Drupal
  • Drupal 8
  • Drupal 9
  • Drupal module
  • Module Development
  • Drupal
  • Code
Less than 1 minute

Delete multiple nodes in Drupal

$content_type = 'content_type_name';
$nids = \Drupal::entityQuery('node')
  ->condition('type', $content_type)
  ->execute();
use  \Drupal\node\Entity\Node;

$nodes = Node::loadMultiple($nids);
foreach($nodes as $node) {
  $node->delete();
}
$storage_handler = \Drupal::entityTypeManager()->getStorage('node');
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities);