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_contententities using Drupal APIs - Support dry-run execution for auditing purposes
- Log cleanup operations through the module logger channel
Steps to reproduce
- Install and enable the Block Content Explorer module
- Create multiple custom block content entities
- Remove block placements or references so some blocks become orphaned
- Run orphan detection logic or inspect orphaned block results
- 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:
OrphanDetectorEntityTypeManagerInterfaceLoggerInterface
The service should provide a public method:
cleanOrphans(int $limit = 50, bool $dry_run = FALSE): intThe implementation should:
- Retrieve orphaned block IDs from the detector service
- Load
block_contententities 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_cleanerAdds a new public method:
cleanOrphans(int $limit = 50, bool $dry_run = FALSE): intData model changes
None.
The implementation uses existing block_content entities and does not introduce schema or storage changes.
Issue fork block_content_explorer-3589419
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 #4
mohnish s. yadavComment #5
mohnish s. yadav