Delete All Entities (node, user, term) by specific conditions in Drupal 8/9

lakshmi , Credit to  volkotech-solutions Jan 06

Deletes multiple entities permanently by using function entity_delete_multiple() in drupal 8.The entity_delete_multiple function has been deprecated since Drupal 8.0.x and will be removed before Drupal 9.0.0. To delete multiple entities, use the delete() method of the entity storage.

Delete all nodes of a specific content type in Drupal 8/9

To load multiple node entities by using \Drupal\node\Entity\Node::loadMultiple() function then delete nodes by looping node entities. Delete multiple nodes of a specific content type by using the delete() method of Drupal::entityTypeManager storage service.

<?php
use  \Drupal\node\Entity\Node;

//article is machine name of Article content type.
$content_type = 'article';
$ids = \Drupal::entityQuery('node')
    ->condition('type', $content_type)
    ->execute();

//Method 1: To delete multiple nodes by looping node entities.
$nodes = Node::loadMultiple($ids);
  foreach($nodes as $node) {
    $node->delete();
  }

//Method 2: To delete multiple nodes by storage handler.
$storage_handler = \Drupal::entityTypeManager()->getStorage('node');
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities);

Delete all users of a specific role in Drupal 8/9

To load multiple node entities by using \Drupal\user\Entity\User::loadMultiple() function then delete users by looping user entities. Delete multiple users of a specific role by using the delete() method of Drupal::entityTypeManager storage service.

<?php
use  \Drupal\user\Entity\User;

//developer is machine name of role.
$role = 'developer';
$ids = \Drupal::entityQuery('user')
    ->condition('roles', $role)
    ->execute();

//Method 1: To delete multiple users by looping user entities.
$users = User::loadMultiple($ids);
foreach($users as $user) {
    $user->delete();
}
//Method 2: To delete multiple users by storage handler.
$storage_handler = \Drupal::entityTypeManager()->getStorage('user');
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities);

Delete all terms of a specific vocabulary in Drupal 8/9

To load multiple terms by using \Drupal\taxonomy\Entity\Term::loadMultiple() function then delete terms by looping taxonomy terms. Delete multiple terms of a specific vocabulary by using the delete() method of Drupal::entityTypeManager storage service.

<?php
use \Drupal\taxonomy\Entity\Term;

//languages is machine name of vocabulary.
$vocabulary_name = 'languages';
$ids = \Drupal::entityQuery('taxonomy_term')
   ->condition('vid', $vocabulary_name)
   ->execute();


//Method 1: To delete multiple terms by looping taxonomy terms.
 $terms = Term::loadMultiple($ids);
  foreach($terms as $term) {
    $term->delete();
  }

//Method 2: To delete multiple terms by storage handler.
$storage_handler = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities)

Delete all entities by using the devel module

In Drupal 8/9, delete all entities by using the Devel module. For example, delete all nodes of a specific content-type:

  • Navigate to the Admin -> configuration -> development -> Generate content (admin/config/development/generate/content )
  • Choose the content type which nodes you want to delete.
  • Select the “Delete all content in these content types before generating new content” option.
  • Put “0” in “How many nodes would you like to generate?”
  • Click the “Generate” button.

Conclusion:

That's all! You have successfully deleted all entities (node, user, term) by specific conditions and used the storage handler method in Drupal 8/9.

Comments