Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 8: delete all nodes of the same type

Tags:

drupal-8

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!

like image 597
megastruktur Avatar asked Jan 04 '16 14:01

megastruktur


People also ask

How to delete nodes on Drupal?

Go to Administer -Content to see the list of all the nodes on your site. Filter the list to show just the one content_type. Select one node and delete it. This triggers your Rule and all the nodes are deleted.

How do you remove a node?

If the node to be deleted is the head node, then simply point the head to the second node of the linked list. For all other nodes: Traverse the linked list and for the current node curr, check whether the next node contains the key that needs to be deleted.


4 Answers

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.

like image 173
colan Avatar answered Nov 05 '22 00:11

colan


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

like image 34
Nysso Avatar answered Nov 05 '22 00:11

Nysso


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);
like image 10
mouhammed Avatar answered Nov 05 '22 01:11

mouhammed


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.

like image 8
megastruktur Avatar answered Nov 04 '22 23:11

megastruktur