I need to bulk delete the duplicate nodes based on the same field and keep just one node.

How can I do that ?

I tried that :

$query = \Drupal::entityQuery('node')->condition('type', "diffusion");
//$query->having('COUNT(field_id_diffusion) >= 1');
$duplicate = $query->execute();
  • how many nodes do you have?
    – Yuseferi
    Mar 22 '17 at 19:37
  • Depending on the count of nodes, it might be best to do this as a batch op. Or could you use Views, add the field as a exposed filter, and bulk delete the result list?
    – Kevin
    Mar 22 '17 at 19:51

If your nodes are not too many you can achieve it by something like following :

$query = \Drupal::entityQuery('node')->condition('type', "diffusion");
$nids = $query->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

$diffusion_checklist = [];
foreach ($nodes as $node) {
   $diff_id = $node->get('field_id_diffusion')->value;

   if (!in_array($diff_id ,$diffusion_checklist)) {
     $diffusion_checklist[] = $diff_id;
   else {

And if you have too many nodes another solution is implement the same strategy with setup a batch operations

  • Sorry for my late answer. We have 80.000 nodes to check => I don't know what's your definition of too many but it's kinda huge for me...
    – injetkilo
    Mar 26 '17 at 19:16
  • @injetkilo my solution is worked, 80,000 is not too many. just set max_execution_time to something like 360 - 720 temporary ( it's dependence on your CPU and Memory limit).
    – Yuseferi
    Mar 26 '17 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.