Drupal 8:删除相同类型的所有节点

meg*_*tur 13 drupal-8

我需要删除Drupal 8中相同类型的所有节点(节点超过7k).

这对Drupal 7来说不是问题(DB query + node_delete或node_delete_multiple会解决我的问题).但是,D8略有不同:)

请,建议,我该怎么做.提前致谢!

col*_*lan 20

应该使用实体查询而不是直接对数据库执行操作:

  $result = \Drupal::entityQuery('node')
      ->condition('type', 'my_content_type_name')
      ->execute();
  entity_delete_multiple('node', $result);
Run Code Online (Sandbox Code Playgroud)

像其他答案那样设置范围应该不会太困难.

有关更多信息,请参阅EntityFieldQuery已被重写.

  • entity_delete_multiple 已弃用,在 Drupal 9.0.0 之前将被删除。从长远来看,不要使用它请参阅:https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/entity_delete_multiple/8.2.x (2认同)

meg*_*tur 7

嗯,答案就在于表面:

$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);
Run Code Online (Sandbox Code Playgroud)

我建议你使用"范围"和某种"批处理"(或者只是多次重新运行代码),因为它是一个非常胖的操作(每个操作500个节点可以用于256MB).

要执行此代码,您可以编写自定义模块或使用devel模块:https://www.drupal.org/project/devel

安装完成后,转到yoursite_address/devel/php并在那里执行php代码.


小智 6

Drupal 8具有按内容类型获取节点的功能,因此我会使用

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

foreach ($nodes as $node) {
    $node->delete();
}
Run Code Online (Sandbox Code Playgroud)


mou*_*med 6

从Drupal 8.0.x开始不推荐使用entity_delete_multiple,它将在Drupal 9.0.0之前删除。使用实体存储的delete()方法删除多个实体:

// 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);
Run Code Online (Sandbox Code Playgroud)


小智 6

您可以使用Devel模块
1-进入管理->配置->开发->生成内容
(admin / config / development / generate / content)
2-选择要删除其节点的内容类型。
3- 选中 “删除这些内容类型中的所有内容。”(重要)
4- 在“您要生成多少个节点”(重要)中输入“ 0 ”(重要),
请参见附件中的说明。

附加图片