What are the steps required to reproduce the bug?

Given a content type with an entity reference field...
1. create node A and node B
2. create node C, that references node A and node B (in that order)
3. delete node A
4. node C still has a reference to node A in DB, and you'll see an empty spot above the reference to node B where the reference to node A used to be

What behavior were you expecting?

Any references to node A would be removed once node A is deleted.

What happened instead?

Node C still has a reference to node A, even though node A no longer exists.

Comments

moonray created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dzinkevich’s picture

Hi, I'm also seeing this behavior. We're using 8.1.8 over here. We'll be looking into this shortly.

jonathanshaw’s picture

I suggest this is major because it undermines data integrity.

This issue has a long history on D7: see #1368386: Delete references to deleted entities. There is no consensus there on how to fix it, because of the performance trade off - people want it:
- immediate (so the broken references don't persist for some unknown period of time)
- thorough (with hooks and consequences, so that other relationships in the data can be maintained)
- scalable (because someone might try to delete 100000 nodes in one go)

Because of the need to be thorough, it is impossible to be both scalable and immediate.

This is the identical dilemma found in #89181: Use queue API for node and comment, user, node multiple deletes.

berdir’s picture

We did provide a special solution for this in https://www.drupal.org/node/2390467 for user roles. The idea there was to use the same approach here by adding a generic method to the storage.

That might still be possible if we make it optional. However, it would be possible to go through the hooks.

jonathanshaw’s picture

csedax90’s picture

There's no solution for now?

nghai’s picture

Is there any update on the issue ?

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rooby’s picture

Title: When deleting entity references to deleted entity remain for entity reference fields » When deleting an entity, references to the deleted entity remain in entity reference fields
gambry’s picture

Because of the need to be thorough, it is impossible to be both scalable and immediate.

This is the identical dilemma found in #89181: Use queue API for node and comment, user, node multiple deletes.

Won't set the field record deleted column to 1 address both scalable (deletion will be silently done by cron) and immediate (field won't show up on UI)?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xano’s picture

I was notified of this issue after I announced the availability of Entity Reference Guards. It aims to provide a rough framework for all kinds of entity reference handling, including reference clean-up and protection. Plans are made to support deferred guards, but those have not been implemented yet.

dawehner’s picture

It looks like https://www.drupal.org/project/erg has the potential to solve this particular issue

handkerchief’s picture

any news about this?

Is https://www.drupal.org/project/erg the workaround for this or will this fixed in the core?

It's a really important function! It makes absolutely no sense if that is not the normal behavior.

dawehner’s picture

Won't set the field record deleted column to 1 address both scalable (deletion will be silently done by cron) and immediate (field won't show up on UI)?

I think the issue is that we don't really expose anything like that on the entity storage API yet.

handkerchief’s picture

@dawehner and what does that mean in concrete terms?

cola’s picture

hi all, same issue here... any idea how to solve this issue? is critical for me!

mxh’s picture

You could try out Entity Reference Guards and report back here what you're missing in detail, which could or should be a part of Core's entity reference functionality.

For me, the current behavior of Entity Reference makes sense. It's actually helpful, e.g. when you want to restore accidentally deleted entities and having their relations back. People regularly get confused about this behavior, especially when they come from a domain where referential integrity seems to be something "natural", which is not. Implementing this on application-level is a torture though. You won't be able to satisfy anyone, since this is usually a requirement on database-level.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

sleepingmonk’s picture

https://www.drupal.org/project/erg Says it's abandoned and there are virtually no installs. Is there some other solution in the works? Is something coming in core? Where should efforts be directed to help with a solution?

mxh’s picture

Despite of the unsupported status, you can still verify whether this module might be suitable. If so, or at least if it already has implemented a lot to achieve the goal, then it can still be "revived" (thanks to GPLv2).

Referential integrity on database level will most likely not being supported by core Drupal 8, as well as Drupal 9.

blazey’s picture

This problem is quite painful, as legitimate actions performed by editors break facets. I think we could try to solve the issue by splitting it into smaller pieces, for example:

The latter could probably be split further. When the deletion is triggered from the browser, eg. via the confirmation form, we could add a checkbox which would start the cleanup batch right away. For a non-interactive deletion (eg. PHP API, REST API) it might make sense to use cron queue workers.

jonathanshaw’s picture

Splitting up is great. But isn't #2978521: Remove orphaned (dangling) entity references when an entity is deleted just a duplicate of this issue? The title is basically identical...

blazey’s picture

Title: When deleting an entity, references to the deleted entity remain in entity reference fields » [META] Clean up references to deleted entities

That's true. This issue represents a broader problem, so turning it into meta.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

prineshazar’s picture

+ Subscribing, this is an issue for us when using GraphQL entity reference fields, data for orphaned references are null value, which isnt helpful as its not usable Graph information.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

boby_ui’s picture

I am having the same issue where when I delete the parent node reference, all the references are not deleted, is there any way we can trigger a entity delete when a parent reference is deleted?

jonathanshaw’s picture

is there any way we can trigger a entity delete when a parent reference is deleted?

Yes, but this isn't the best place for a general support question like that; I suggest Drupal Answers.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pfrenssen’s picture

For reference, a similar functionality is implemented in Organic Groups to clean up orphaned group content. The implementation uses a plugin type for choosing the way to perform the purging of the orphans, with three plugins:

  1. Batch: A batch process that can be triggered in the UI.
  2. Cron: Clean up orphans in a cron job.
  3. Simple: Cleans up orphans immediately, with an associated performance penalty.

On hook_entity_predelete() the entity being deleted is registered in the plugin manager for processing, ref og_entity_predelete(). There is a DeleteOrphan QueueWorker and the test coverage is in OgDeleteOrphansTest.

The main difference is that in this issue we would be cleaning up reference fields, while in the OG implementation it removes the referenced entities.

claudiu.cristea’s picture

I like the idea of a plugin system, as in OG. More, I think the plugin should be configurable on field config level. Some fields might want to do a Simple cleanup, while others on Cron.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

moshe weitzman’s picture

I also think that OG approach is terrific. But if we dont get there, I think its OK. We don't do any reference cleanup at all today. So even a slow and scalable implementation is an improvement (i.e. queue+cron), and would work on all sites.

This reference pruning will add a lot of new entity saves and thus a lot of new cache invalidation. Sites that are running hot will struggle even more. I think thats OK - referential integrity is more important. Just want to point out this consequence.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Category: Bug report » Plan

Moving this to a plan.

fwiw I think a queue would be fine, and we shouldn't make this configurable - if we want to add batch support we should add some kind of AJAX queue runner that can burn down queues.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jurgenhaas’s picture

I have a different perspective on this topic, and we implement the following approach on our customer projects: Never allow content entities from being deleted!

The reason behind this: with workflow and content moderation, we have a great tool to keep track on what happened with content, when, and by whom. However, when any content entity would get deleted, that trail would be lost as well. Which is not what people want. Content, which should "disappear" gets archived, i.e. unpublished and transitioned into an appropriate moderation state. With the correct configuration of permissions, such content appears as if it were deleted to most user roles.

Instead of trying to "improve" the data integrity when referenced entities get deleted, which will never be fully correct to everyone, I wish Drupal came with a mode, where the D in CRUD were not available.

jonathanshaw’s picture

#42 sounds like https://www.drupal.org/project/trash. @timmillwood did a lot of work on exploring the feasability of this kind of approach in core.

But that's a distraction from this issue: whether a referenced entity is archived or deleted, there's still the question of how to handle the outstanding reference.

dalin’s picture

Even though this issue is listed as the "Meta" issue, it's child #2978521: Remove orphaned (dangling) entity references when an entity is deleted has a more thorough summary, suggested alternatives, and also outlines scenarios where you _don't_ want automatic clean-up (so we need to allow both). So I wonder if we should close out this issue in favour of the 2978521???

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.