To delete a path alias we use the entity type manager to load the path_alias entity type storage and then use that to load the path using the alias.
$pathAliasStorage = \Drupal::entityTypeManager()->getStorage('path_alias');
// Load path alias by path.
$aliasEntities = $pathAliasStorage->loadByProperties([
'path' => '/node/' . $nid
]);
foreach ($aliasEntities as $aliasEntity) {
// Delete the path alias.
$aliasEntity->delete();
} If the original page was published then you need to make sure you have a redirect or similar to ensure that the path resolves correctly.
Add new comment