If you get an error that looks like this while saving a node or a taxonomy term in Drupal, it may mean that the table that stores path aliases in the database has become corrupted for some reason.
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23505]: Unique violation: 7
ERROR: duplicate key value violates unique constraint "path_alias____pkey" DETAIL:
Key (id)=(30) already exists.: INSERT INTO "path_alias" ("revision_id", "uuid",
"langcode", "path", "alias", "status") VALUES (:db_insert_placeholder_0,
:db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3,
:db_insert_placeholder_4, :db_insert_placeholder_5) RETURNING id; Array ( ) in
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of
/home/www-data/packages/drupal/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Since aliases can always be regenerated (if you are using the Pathauto module), you can safely delete them. So, to fix the error truncate both path alias tables:
$tables = [
'path_alias',
'path_alias_revision',
];
foreach ($tables as $table) {
\Drupal::database()->truncate($table)->execute();
}
then proceed to the Bulk generate page (admin/config/search/path/update_bulk) and generate path aliases again.
Before doing this make sure that you don't have any manually entered ('Generate automatic URL alias' unchecked) path aliases.
Warning: If you have manually entered path aliases then don't use this method to fix the error, because you will lose content that can't be regenerated automatically.
About the Author
Goran Nikolovski is a senior web developer who loves working with Drupal, React, and React Native. He is the founder of this website and he enjoys sharing his knowledge. Need help with anything? Get in touch.