Problem/Motivation
On a project content_moderation_state entities were left behind after their nodes were deleted. I let Claude investigate and it found the cause.
EntityOperations::entityDelete() looks up the state to delete with ContentModerationState::loadFromModeratedEntity(), which matches on the loaded revision ID (core/modules/content_moderation/src/Entity/ContentModerationState.php:145). If that revision was deleted in the meantime, nothing is found and the state survives its host.
This happens when a user is deleted: NodeEntityHooks::userPredelete() deletes the node revisions they authored. A node owned by someone else stays, and deleting it later through an object loaded before leaves the state behind. drupal-test-traits triggers this routinely, since it deletes the user created in setUp() before the nodes it revised.
Steps to reproduce
With a user deletion:
- User A creates a moderated node. Load it as
$stale. - Change the author to user B and save it as a new revision.
- Delete user A. The node stays, but its first revision is deleted.
$stale->delete()
Minimal case:
- Create a moderated node, load it as
$stale. - Save a new revision of the node.
$storage->deleteRevision($stale->getLoadedRevisionId())$stale->delete()
Result in both cases: the node is gone, its content_moderation_state remains. Both reproduced on 11.x with kernel tests, main has the same code.
Proposed resolution
When the whole entity is deleted the loaded revision doesn't matter. Let's delete every content_moderation_state with the content_entity_type_id and content_entity_id of the deleted entity, for all workflows and languages. Add a kernel test following the steps above.
Remaining tasks
MR, review.
User interface changes
None.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Release notes snippet
Related issues
- #2893888: Content moderation state entity field data not removed when the host field data is
- #3181439: Content Moderation fatals when a moderated entity is re-saved on hook_insert()
AI disclosure: used Claude to discover the root of the problem, find replication steps, and create the initial draft of the bug report.
Issue fork drupal-3641921
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
pfrenssenComment #3
pfrenssen