Problem/Motivation

The Block Content Explorer module can identify orphaned custom block content entities, but currently there is no dedicated cleanup service responsible for safely removing them.

As orphaned blocks accumulate over time, they increase unnecessary database content and make long-term content maintenance more difficult for site administrators.

A reusable cleanup service is needed to:

  • Retrieve orphaned block IDs from the OrphanDetector service
  • Safely delete orphaned block_content entities using Drupal APIs
  • Support dry-run execution for auditing purposes
  • Log cleanup operations through the module logger channel

Steps to reproduce

  1. Install and enable the Block Content Explorer module
  2. Create multiple custom block content entities
  3. Remove block placements or references so some blocks become orphaned
  4. Run orphan detection logic or inspect orphaned block results
  5. Observe that orphaned blocks can be detected but there is no reusable cleanup service to remove them

Proposed resolution

Add a new cleanup service:

Drupal\block_content_explorer\Service\OrphanCleaner

Register the service in block_content_explorer.services.yml and inject:

  • OrphanDetector
  • EntityTypeManagerInterface
  • LoggerInterface

The service should provide a public method:

cleanOrphans(int $limit = 50, bool $dry_run = FALSE): int

The implementation should:

  • Retrieve orphaned block IDs from the detector service
  • Load block_content entities through entity storage
  • Delete entities using
    $block->delete()
  • Skip blocks that no longer exist
  • Support dry-run mode without performing deletions
  • Log cleanup operations using the module logger channel
  • Return the number of processed orphaned blocks

Remaining tasks

  • Create src/Service/OrphanCleaner.php
  • Register the service in block_content_explorer.services.yml
  • Add constructor dependency injection
  • Implement orphan cleanup logic
  • Add dry-run support
  • Add logging for cleanup operations
  • Test service behavior using drush php-eval
  • Verify log entries appear in Recent log messages

User interface changes

None.

This issue introduces backend cleanup functionality only.

API changes

Adds a new service:

block_content_explorer.orphan_cleaner

Adds a new public method:

cleanOrphans(int $limit = 50, bool $dry_run = FALSE): int

Data model changes

None.

The implementation uses existing block_content entities and does not introduce schema or storage changes.

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

mohnish s. yadav created an issue. See original summary.

oshi02 made their first commit to this issue’s fork.

mohnish s. yadav’s picture

Status: Active » Needs review
mohnish s. yadav’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.