Drupal 8: delete all nodes of the same type

I need to delete all nodes of the same type in Drupal 8 (there are more than 7 thousand nodes).

This is not a problem for Drupal 7 (DB query + node_delete or node_delete_multiple would solve my problem). However, the D8 is a little different :)

Please advice on how I can do this. Thanks in advance!

+6
source share
8 answers

You need to use entity queries, and not act directly in the database:

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

Setting ranges, as in the other answer, should not be too complicated.

See EntityFieldQuery has been rewritten for more information.

+9
source

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 advise you to use a "range" and some kind of "batch" (or just re-run the code several times), because it is a very thick operation (500 nodes per operation in order for 256 MB).

To execute this code, you can write your own module or use the devel module: https://www.drupal.org/project/devel p>

After installation, go to yoursite_address / devel / php and execute the php code there.

+7
source

Drupal 8 has functionality for retrieving 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(); } 
+5
source

The entity_delete_multiple object is deprecated, like Drupal 8.0.x will be removed to Drupal 9.0.0. Use the delete () method of the object store to delete multiple objects:

 // 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); 
+3
source

you can use the devel module
1 - go to Admin-> configuration-> development-> Create content
(admin / config / development / generate / content)
2 - select the type of content you want to delete. 3 - check the box "delete all content in these types of content .." (important)
4 - enter "0" in "how many nodes would you like to generate" (important)
see attached image for instructions.

attached image

+2
source

To remove all entities of some entity types, I use this snippet adapted from the 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
source

A very simple way is to install Bulk Delete . It is available for D7 and D8.

After installing the module, you will see the Bulk delete node tab when you click on the content menu.

It saved my day :)

For your ease, I added a screenshot.

enter image description here

0
source

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

drupal entity:delete [arguments]

0
source

All Articles