You are here

function delete_all_entities_batch_delete in Delete all 2.x

Same name and namespace in other branches
  1. 8 includes/delete_all.entities.batch.inc \delete_all_entities_batch_delete()

Function to delete content using Batch API.

Parameters

array $entities: Array of entities to delete.

array &$context: Sandbox context array.

1 string reference to 'delete_all_entities_batch_delete'
EntityDeleteController::getEntitiesDeleteBatch in src/Controller/EntityDeleteController.php

File

includes/delete_all.entities.batch.inc, line 11

Code

function delete_all_entities_batch_delete($entities = FALSE, $entity_type, &$context) {
  $db = \Drupal::database();
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($entities);

    // Collect results to process in the finished callback.
    $context['results']['count'] = $context['sandbox']['max'];
  }

  // Get a batch of 100 nodes to delete.
  $to_delete = array_slice($entities, $context['sandbox']['progress'], 100);
  if ($context['sandbox']['max'] + 1 > 0) {
    if (!empty($to_delete)) {
      foreach ($to_delete as $id) {
        $storage = \Drupal::entityTypeManager()
          ->getStorage($entity_type);
        $entity = $storage
          ->load($id);

        // taxonomy terms and other entities are hierarchic, causing some entities to be deleted when their parents are deleted
        if ($entity) {
          $entity
            ->delete();
        }
        $context['message'] = t('Deleting entity with id %id', [
          '%id' => $id,
        ]);
        $context['sandbox']['current_nid'] = $id;
        $context['sandbox']['progress']++;
      }
    }

    // Inform the batch engine that we are not finished,
    // and provide an estimation of the completion level we reached.
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
}