'The alias is already in use' when editing a node that had an alias generated programmatically I'm currently using an event subscriber to latch onto an event from a module that creates and/or updates nodes. I'm programmatically setting the url alias based on a user inputted field by doing the following

$path = \Drupal::service('path.alias_storage')-&gt;save("/node/" . $node-&gt;id(), $url_alias, "en");


It works but when I open the node and attempt to edit it, I get the message 


  The alias is already in use


When I try to save it. Is there something I'm missing? Does a field on the actual node have to be edited as well?

I'm also using pathauto if that has any effect.

UPDATE: I just noticed that whenever that aliased is saved it generates 4 entries in the URL alias list.
Turns out whenever you perform this

\Drupal::service('path.alias_storage')-&gt;save("/node/" . $node-&gt;id(), $url_alias, "en");


It generates a new alias every time even if the path alias already exists. I was able to resolve the issue by performing checks to see if the path already exists like so:

if (!\Drupal::service('path.alias_storage')-&gt;pathHasMatchingAlias('/node/' . $node-&gt;id()) &amp;&amp; !empty($node-&gt;id())) {
    $path = \Drupal::service('path.alias_storage')-&gt;save("/node/" . $node-&gt;id(), $url_alias, "en");
}