Drupal 8: delete all nodes of the same type

微笑、不失礼 提交于 2019-12-06 18:29:53

问题


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!


回答1:


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.




回答2:


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.




回答3:


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();
}



回答4:


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);



回答5:


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




回答6:


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.




回答7:


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);
}



回答8:


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

drupal entity:delete [arguments]



来源:https://stackoverflow.com/questions/34593060/drupal-8-delete-all-nodes-of-the-same-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!