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:

  1. User A creates a moderated node. Load it as $stale.
  2. Change the author to user B and save it as a new revision.
  3. Delete user A. The node stays, but its first revision is deleted.
  4. $stale->delete()

Minimal case:

  1. Create a moderated node, load it as $stale.
  2. Save a new revision of the node.
  3. $storage->deleteRevision($stale->getLoadedRevisionId())
  4. $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

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

Command icon 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

pfrenssen created an issue. See original summary.

pfrenssen’s picture

Issue summary: View changes
pfrenssen’s picture

Version: 11.x-dev » main