Drupal 8: delete all nodes of the same type

后端 未结 9 1930
予麋鹿
予麋鹿 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:29

    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

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

    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.

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

    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.

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