Delete any path alias by language We are running Drupal 8.8.5. I can see duplicate aliases when I look at /admin/config/search/path.

Unfortunately, there are many duplicate aliases on our website with 6000 URLs. The /node/1 node has three translations: en, es, and zh-hans.  /node/1 also has a second entry for es containing a duplicate path alias which I would like to delete.

I need to find a way to programmatically delete path aliases. If I have Create, Read, Update and Delete (CRUD) operations on the path/alias/langcode, I can create a solution to eliminate the duplicates. 

The following code obtains the alias data associated with the node whose ID is equal to 1 and dumps the Path, Alias and LangCode.

$alias = \Drupal::entityTypeManager()-&gt;getStorage('path_alias')-&gt;load('1');
var_dump($alias-&gt;getPath());
var_dump($alias-&gt;getAlias());
var_dump($alias-&gt;language()-&gt;getId());


The base language of the website is English, and the last var_dump() yields en.  So far so good.

How can I gain access to the alias data for these two languages?  I know the following works.

$alias_es = \Drupal::service('path_alias.manager')-&gt;getAliasByPath('/node/1','es');


I need to delete the duplicate Spanish path alias to accomplish my goal, so I need to figure out how to use entities which have a delete() method.  You might think the following code should work.

$alias_es = $alias-&gt;getTranslation('es');


It returns:


  InvalidArgumentException: Invalid translation language (es) specified.


I also tried $alias-&gt;getTranslationLanguages(), which returns an array with only one element (['en'] =&gt; object(Drupal\Core\Language\Language)).  

It appears that our original $alias = \Drupal::entityTypeManager()-&gt;getStorage('path_alias')-&gt;load('1'); only has one language translation which is English. 

How can I gain access to the path aliases for es and zh-hans?
How can I gain access to the path aliases for es and zh-hans?


To get all path alias by language for instance es: 

$path_alias_manager = \Drupal::entityTypeManager()-&gt;getStorage('path_alias');
// Load all path alias for this node for es language.
$alias_objects = $path_alias_manager-&gt;loadByProperties([
  'path' =&gt; '/node/' . $nid,
  'langcode' =&gt; 'en'
]);



  I need to delete the duplicate Spanish path alias


$path_alias_manager = \Drupal::entityTypeManager()-&gt;getStorage('path_alias');
// Load all path alias for this node for es language.
$alias_objects = $path_alias_manager-&gt;loadByProperties([
  'path'     =&gt; '/node/' . $nid,
  'langcode' =&gt; 'es',
]);

// Get the actual associated spanish path alias to your node.
$spanish_associated_alias = \Drupal::service('path.alias_manager')-&gt;getAliasByPath('/node/' . $nid, 'es');
foreach ($alias_objects as $alias_object) {
  // If its not the actual alias delete it.
  if ($alias_object-&gt;get('alias')-&gt;value !== $spanish_associated_alias ){
    $alias_object-&gt;delete();
  }
}