Setup
Drupal 9 site has Users and each user has a Paragraph that contains "Phone" data.
User => Phone Paragraph connection is made via field $user->field_phone_para
.
Issue
Somehow database has gotten "ghost" references for the $user->field_phone_para
field.
When I load the user and print the phone paragraph reference values/targets, it gets 3 responses. In reality there should be only one.
$user = User::load(4049);
dd($user->field_phone_para->getValue());
Outputs:
array:3 [
0 => array:2 [
"target_id" => "8555"
"target_revision_id" => "15528"
]
1 => array:2 [
"target_id" => "17677"
"target_revision_id" => "36696"
]
2 => array:2 [
"target_id" => "26289"
"target_revision_id" => "45308"
]
]
user__field_phone_para
database table confirms the same - 3 entries for this user.
Yet if I check the paragraph__field_phone
database table for those 3 target_id-s , there is entry only for the ID 26289
. Which means that 8555 and 17677 are kind of ghosts and should be gone.
Question
What is the correct way to clean up the database from those ghost / orphaned entries?
Ideas / what I've tried
- Manually saving the user in the Drupal admin removes the ghost references (but not viable due to the amount of users).
- Just loading the user and then saving the user via
save()
does not remove the ghost references.
- Tested https://www.drupal.org/project/entity_reference_purger - does not seem to work with Users + Paragraphs.
Would it make sense to have a SQL query to delete all the entries from the user__field_phone_para
database table which don't have corresponding entries in the paragraph__field_phone
table? Matching would be done by user__field_phone_para.field_phone_para_target_id
and paragraph__field_phone.entity_id
.
Only possible issue with deleting directly via the SQL query is that the "delta" would not be reset in the user__field_phone_para
table to what it should be - in the current example case it should become 0 for the real entry since we remove 2 ghosts. Yet when new phone number is added via the admin form, Drupal would take care of resetting the delta anyway so probably not an issue.