Created
June 22, 2015 07:14
-
-
Save Jaesin/b4e6b8331671e231b9e8 to your computer and use it in GitHub Desktop.
Delete all content entities in Drupal 8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Delete all nodes. | |
entity_delete_multiple('node', \Drupal::entityQuery('node')->execute()); | |
// Delete all files. | |
entity_delete_multiple('file', \Drupal::entityQuery('file')->execute()); | |
// Delete all taxonomy terms. | |
entity_delete_multiple('taxonomy_term', \Drupal::entityQuery('taxonomy_term')->execute()); | |
// Delete all block content. | |
entity_delete_multiple('block_content', \Drupal::entityQuery('block_content')->execute()); | |
// Delete all menu links. | |
entity_delete_multiple('menu_link_content', \Drupal::entityQuery('menu_link_content')->execute()); | |
// Delete all users except 1 and 0. | |
entity_delete_multiple('user', \Drupal::entityQuery('user')->condition('uid', '1', '!=')->condition('uid', '0', '!=')->execute()); | |
An alternative way:
$ids = \Drupal::entityQuery('node')->execute();
$controller = \Drupal::entityTypeManager()->getStorage('node');
$entities = $controller->loadMultiple(ids);
$controller->delete($entities);
$ids = \Drupal::entityQuery('node')->execute(); -> can be removed
$controller = \Drupal::entityTypeManager()->getStorage('entity_type');
$entities = $controller->loadMultiple(); -> load all entities, if no $ids
$controller->delete($entities);
For deleting the comment
entity_delete_multiple('comment', \Drupal::entityQuery('comment')->execute());
This is also a great way to increase the performance of your Drupal site.
For deleting custom entities:
$storage_handler = \Drupal::entityTypeManager()->getStorage('ENTITY_ID');
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities);
@Jaesin – Can you maybe update the Gist? Because entity_delete_multiple
is deprecated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this, however, it seems that some of these are deprecated. Do you have a more updated approach to deleting these entities?