You are here

public function PathautoCommands::deleteAliases in Pathauto 8

Delete URL aliases

@command pathauto:aliases-delete

@option purge Deletes all URL aliases, including manually created ones.

@usage drush pathauto:aliases-delete canonical_entities:node Delete all automatically generated URL aliases for node entities, preserving manually created aliases. @usage drush pathauto:aliases-delete all Delete all automatically generated URL aliases, preserving manually created ones. @usage drush pathauto:aliases-delete all --purge Delete all URL aliases, including manually created ones. @usage drush pathauto:aliases-delete When the alias types are omitted they can be chosen from an interactive menu.

@aliases pad

Parameters

array $types: Comma-separated list of alias types to delete. Pass "all" to delete aliases for all types.

Throws

\Exception

File

src/Commands/PathautoCommands.php, line 135

Class

PathautoCommands
Drush commands allowing to perform Pathauto tasks from the command line.

Namespace

Drupal\pathauto\Commands

Code

public function deleteAliases(array $types = NULL, $options = [
  'purge' => FALSE,
]) {
  $delete_all = count($types) === count($this
    ->getAliasTypes());

  // Keeping custom aliases forces us to go the slow way to correctly check
  // the automatic/manual flag.
  if (!$options['purge']) {
    $batch = [
      'title' => dt('Bulk deleting URL aliases'),
      'operations' => [
        [
          'Drupal\\pathauto\\Form\\PathautoAdminDelete::batchStart',
          [
            $delete_all,
          ],
        ],
      ],
      'finished' => 'Drupal\\pathauto\\Form\\PathautoAdminDelete::batchFinished',
    ];
    foreach ($types as $type) {
      $batch['operations'][] = [
        'Drupal\\pathauto\\Form\\PathautoAdminDelete::batchProcess',
        [
          $type,
        ],
      ];
    }
    batch_set($batch);
    drush_backend_batch_process();
  }
  elseif ($delete_all) {
    $this->aliasStorageHelper
      ->deleteAll();
    $this
      ->logger()
      ->success(dt('All of your path aliases have been deleted.'));
  }
  else {
    foreach ($types as $type) {

      /** @var \Drupal\pathauto\AliasTypeInterface $alias_type */
      $alias_type = $this->aliasTypeManager
        ->createInstance($type);
      $this->aliasStorageHelper
        ->deleteBySourcePrefix($alias_type
        ->getSourcePrefix());
      $this
        ->logger()
        ->success(dt('All of your %label path aliases have been deleted.', [
        '%label' => $alias_type
          ->getLabel(),
      ]));
    }
  }
}