Score:1

Removing orphaned paragraph references from the user

ir flag

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.

cn flag
I read this wrong and put in an answer about using the /admin/config/system/delete-orphans feature (which cleans up orphaned entities, the opposite of your problem), but that might still be worth a try. It might also clean up the field references
4uk4 avatar
cn flag
@Raido, you are asking how to remove the ghost references. There is probably no existing code because this normally doesn't happen. My guess would be you have contrib or custom code deleting paragraphs instead of deleting field items. Paragraphs are not deleted right away, most times they are used in revisionable entities and you need them in case of reverting to an older revision. For the non-revisionable user entity there could be code trying to do it differently and there you should look for the issue.
Score:0
fr flag

What is the correct way to clean up the database from those ghost / orphaned entries?

Use drush errp to clean up orphaned paragraph entities.

Make sure to have a database backup before running the above command.

Raido avatar
ir flag
Tested the ```drush errp``` and the path that @Clive mentioned. It checked for 47546 paragraphs but did remove only 9 and database still has the ghost entries in the ```user__field_phone_para``` table.
cn flag
`errp` is the drush version of that admin page. Seems it only clears up the orphaned entities, not any fields referencing non-existent entities. I think a custom script is the solution here. Use a batch in an update hook, don't use SQL statements, it's never as easy as it looks
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.