Drupal 8: delete all nodes of the same type

后端 未结 9 1892
予麋鹿
予麋鹿 2021-02-19 01:34

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_del

相关标签:
9条回答
  • 2021-02-19 02:03

    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);
    
    0 讨论(0)
  • 2021-02-19 02:11

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

    drupal entity:delete [arguments]

    0 讨论(0)
  • 2021-02-19 02:19

    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();
    }
    
    0 讨论(0)
  • 2021-02-19 02:22

    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);
    
    0 讨论(0)
  • 2021-02-19 02:24

    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);
    }
    
    0 讨论(0)
  • 2021-02-19 02:28

    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.

    0 讨论(0)
提交回复
热议问题