You are here

function drush_entity_bulk_delete in Entity Bulk Delete 7

Command callback for drush entity-bulk-delete

Parameters

string $entity_type: The entity type.

File

./entity_bulk_delete.drush.inc, line 40
Drush integration for the Entity Bulk Delete module.

Code

function drush_entity_bulk_delete($entity_type) {
  $info = entity_get_info($entity_type);
  if (empty($info) || empty($info['base table']) || empty($info['entity keys']['id'])) {
    return drush_set_error('ENTITY_BULK_DELETE_INVALID_ENTITY_TYPE', dt("Invalid entity type @type.", array(
      '@type' => $entity_type,
    )));
  }
  $bundles = drush_get_option('bundles', '');
  $bundles = !empty($bundles) ? explode(',', $bundles) : array();
  if (!empty($bundles)) {
    if ($invalid_bundles = array_diff($bundles, array_keys($info['bundles']))) {
      return drush_set_error('ENTITY_BULK_DELETE_INVALID_BUNDLES', dt("Invalid bundles for entity type @type: @bundles", array(
        '@type' => $entity_type,
        '@bundles' => implode(', ', $invalid_bundles),
      )));
    }
  }
  $limit = (int) drush_get_option('limit', 25);
  $queue = (bool) drush_get_option('queue');
  $query = _entity_bulk_delete_query($entity_type, $bundles);
  $count_query = clone $query;
  $count = $count_query
    ->count()
    ->execute();
  if (empty($count)) {
    return dt("There are no possible @type entities to delete.", array(
      '@type' => $entity_type,
    ));
  }
  else {
    if ($queue) {
      if (!drush_confirm(dt("You are about to queue @count @type entities for deletion. Are you sure?", array(
        '@count' => $count,
        '@type' => $entity_type,
      )))) {
        return drush_user_abort();
      }
    }
    else {
      if (!drush_confirm(dt("You are about to delete @count @type entities. Are you sure?", array(
        '@count' => $count,
        '@type' => $entity_type,
      )))) {
        return drush_user_abort();
      }
    }
  }
  $batch = array(
    'operations' => array(
      array(
        '_entity_bulk_delete_batch',
        array(
          $query,
          $limit,
          $queue,
        ),
      ),
    ),
  );
  batch_set($batch);
  drush_backend_batch_process();
  if ($queue) {
    drush_log("Queued {$count} {$entity_type} entities for deletion.", 'success');
  }
  else {
    drush_log("Deleted {$count} {$entity_type} entities.", 'success');
  }
}