17

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!

megastruktur
  • 593
  • 1
  • 7
  • 9
  • This is a duplicate of https://drupal.stackexchange.com/q/537/3167 The best answer there for Drupal 8 is this one https://drupal.stackexchange.com/a/224226/3167 ```$storage_handler = \Drupal::entityTypeManager()->getStorage("node"); $entities = $storage_handler->loadByProperties(["type" => "YOUR_CONTENT_TYPE_NAME"]); $storage_handler->delete($entities);``` – JamesWilson Dec 05 '19 at 15:07

9 Answers9

24

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.

colan
  • 2,818
  • 2
  • 20
  • 17
  • Wouldn't it be like the same query + additional wrappers? Wouldn't it hit the performance more than DB query, don't you know? – megastruktur Jul 08 '16 at 21:38
  • I don't know for sure, but I'm guessing this approach is actually more performant because it allows for layers of caching within the API that won't activate if making the database call directly. – colan Jul 09 '16 at 04:24
  • This is more performant in the sense that it doesn't set a limit on how many nodes it cleans up in one use. However, you need to make sure php's memory limit is high enough to hold the $result. – Neil Davis Apr 15 '19 at 15:32
  • 3
    entity_delete_multiple is deprecated and will be removed before before Drupal 9.0.0. Dont use this in the long run See: https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/entity_delete_multiple/8.2.x – Augusto May 06 '19 at 03:24
  • Feel free to edit the answer to update it. Thanks! – colan May 24 '19 at 14:45
11

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);
mouhammed
  • 924
  • 8
  • 16
  • That does not answer the question. OP is asking about how to delete **nodes** of a same **type** (bundle). – flaviovs Jun 27 '19 at 21:07
  • The is code also work for this case. Just replace vid by type, tag by your content type and taxonomy_term by node ! – mouhammed Jun 29 '19 at 16:40
11

you can use the Devel module

  1. go to Admin->configuration->development->Generate content
    ( admin/config/development/generate/content )
  2. select the content type you wish to delete its nodes.
  3. check "delete all content in these content types.." (important)
  4. put "0" in "how many nodes would you like to generate" (important)

see attached image for instructions.

attached image

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Nysso
  • 415
  • 4
  • 6
8

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.

megastruktur
  • 593
  • 1
  • 7
  • 9
8

Drupal 8 has the functionality to get nodes by content type, so I would use

$nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties(array('type' => 'your_content_type'));

foreach ($nodes as $node) {
    $node->delete();
}
UnsettlingTrend
  • 452
  • 1
  • 5
  • 20
3

The very easy way is to install Bulk Delete module. It's available for D7 & D8.

After installing the module, you will see the Bulk Delete node tab option when you click on content menu.

It saved my day :)

For your ease, I have attached a screenshot.

enter image description here

npcoder
  • 414
  • 1
  • 5
  • 13
1

To delete all entities of some entity types, i use this snippet adapted from last comment:

$entity_types = ['taxonomy_term','node','menu_link_content',];
foreach ($entity_types as $entity_type) {
  $query = \Drupal::entityQuery($entity_type);
  $ids = $query->execute();

  $storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
  $entities = $storage_handler->loadMultiple($ids);
  $storage_handler->delete($entities);
}
izus
  • 115
  • 5
1

I use Drupal console for this https://docs.drupalconsole.com/ko/commands/entity-delete.html

drupal entity:delete [arguments]

user3365957
  • 308
  • 1
  • 2
  • 11
0

For Drupal 9.0 works fine

  $ids = \Drupal::entityQuery('node')
    ->condition('type', 'article')
    ->execute();

  $storage_handler = \Drupal::entityTypeManager()->getStorage("node");
  $entities = $storage_handler->loadMultiple($ids);
  $storage_handler->delete($entities);