【问题标题】:Drupal 8: delete all nodes of the same typeDrupal 8:删除所有相同类型的节点
【发布时间】:2016-04-08 04:16:54
【问题描述】:

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

这对 Drupal 7 来说不是问题(数据库查询 + node_delete 或 node_delete_multiple 可以解决我的问题)。但是,D8 略有不同:)

请指教,我该怎么做。提前致谢!

【问题讨论】:

标签: drupal-8


【解决方案1】:

您可以使用Devel module

  1. 进入管理->配置->开发->生成内容
    (管理员/配置/开发/生成/内容)
  2. 选择要删除其节点的内容类型。
  3. 勾选“删除这些内容类型中的所有内容..”(重要)
  4. 在“要生成多少个节点”中输入“0”(重要)

有关说明,请参见附图。

attached image

【讨论】:

    【解决方案2】:

    对于 Drupal 9.0 工作正常

      $ids = \Drupal::entityQuery('node')
        ->condition('type', 'article')
        ->execute();
    
      $storage_handler = \Drupal::entityTypeManager()->getStorage("node");
      $entities = $storage_handler->loadMultiple($ids);
      $storage_handler->delete($entities);
    

    【讨论】:

      【解决方案3】:

      我为此使用 Drupal 控制台 https://docs.drupalconsole.com/ko/commands/entity-delete.html

      drupal entity:delete [arguments]

      【讨论】:

        【解决方案4】:

        最简单的方法是安装Bulk Delete 模块。它适用于 D7 和 D8。

        安装模块后,点击内容菜单会看到批量删除节点选项卡选项。

        它拯救了我的一天:)

        为方便起见,我附上了截图。

        【讨论】:

          【解决方案5】:

          要删除某些实体类型的所有实体,我使用这个改编自上次评论的 sn-p:

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

          【讨论】:

            【解决方案6】:

            entity_delete_multiple 自 Drupal 8.0.x 起已弃用,将在 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);
            

            【讨论】:

            • 这不能回答问题。 OP 正在询问如何删除相同 type (bundle) 的 nodes
            • is 代码也适用于这种情况。只需按类型替换 vid,按您的内容类型标记,按节点替换 taxonomy_term !
            【解决方案7】:

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

            $nodes = \Drupal::entityTypeManager()
                ->getStorage('node')
                ->loadByProperties(array('type' => 'your_content_type'));
            
            foreach ($nodes as $node) {
                $node->delete();
            }
            

            【讨论】:

              【解决方案8】:

              应该使用实体查询而不是直接作用于数据库:

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

              像其他答案一样设置范围应该不会太难。

              更多信息请参见EntityFieldQuery has been rewritten

              【讨论】:

              • 不会像同一个查询 + 额外的包装器吗?它不会比数据库查询更能影响性能,你不知道吗?
              • 我不确定,但我猜这种方法实际上性能更高,因为它允许在 API 中进行缓存层,如果直接调用数据库则不会激活。
              • 这在某种意义上更高效,因为它不会限制一次使用时清理的节点数量。但是,您需要确保 php 的内存限制足够高以容纳 $result。
              • entity_delete_multiple 已弃用,将在 Drupal 9.0.0 之前删除。从长远来看,不要使用它请参阅:api.drupal.org/api/drupal/core%21includes%21entity.inc/function/…
              • 随时编辑答案以更新它。谢谢!
              【解决方案9】:

              嗯,答案就在表面上:

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

              我建议您使用“范围”和某种“批处理”(或者只是多次重新运行代码),因为这是一个非常繁重的操作(每次操作 500 个节点对于 256MB 来说是可以的)。

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

              安装后转到 yoursite_address/devel/php 并在那里执行 php 代码。

              【讨论】:

              • 谢谢,真的很好。您也可以使用 drush 8:drush src yourscript.php
              • entity_delete_multiple|() 已被贬低
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-05-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多