A custom integration that finds and deletes orphan entities - entities left in the registry after the integration that created them has been removed or failed - via a web panel in the sidebar and services callable from scripts and automations.
- Open HACS in your Home Assistant instance
- Search for Orphan Entity Cleaner and install
- Restart Home Assistant
- Settings → Devices & Services → Add Integration → Orphan Entity Cleaner
- Copy the
custom_components/orphan_cleaner/folder to<config>/custom_components/ - Restart Home Assistant
- Settings → Devices & Services → Add Integration → Orphan Entity Cleaner
After installation, the Orphan Cleaner icon appears in the HA sidebar. All settings are controlled directly from the panel - no configuration flow needed after the initial setup.
| Control | Description |
|---|---|
| Scan | Scans the registry and populates the table |
| Heuristic | Enables the heuristic detection (see below) |
| Min age (h) | How long an entity must have been unavailable before it is listed (only unavailable entities) |
| Ignore | Add platforms or globs to exclude from the current session |
| Default ignore list | Persistent ignore list, pre-loaded on every scan (see below) |
| Dry run | Simulate deletion without removing anything (see below) |
| Filter | Filter visible results by entity_id or platform |
| Select safe / Deselect | Selects only the entities that are safe to delete and not used anywhere |
| Column headers | Click to sort by Entity ID, Platform, Category or Age - click again to reverse |
| Platform badge | Click on any platform badge to instantly select all entities of that platform |
| Category filter | Dropdown to show only safe, to review or only unavailable entities |
| Delete selected | Delete selected entities with a confirmation dialog |
| Save & Delete | Save a JSON backup of selected entities to /config/orphan_cleaner_backups/ before deleting |
| Remove orphan devices | Shown when devices with no entities and no integration are found |
Type a platform name or glob pattern in the Ignore field in the control bar and press Enter or + Add. Ignored entities remain visible in the table but are greyed out and non-interactive. The orphan counter reflects the exclusion. This list is temporary and resets on page reload.
A collapsible section below the control bar. Entries here are saved to the integration's config entry options and automatically loaded on every scan and page load. Supports:
- Platform names -
tuya,shelly,imou_life - Platform globs -
imou_*,tuya_* - Entity ID globs -
sensor.old_*,*_deprecated,sensor.*_temp
Entries are saved automatically when added or removed - no Save button needed. Use the ? link for a full syntax reference.
Save & Delete exports a timestamped JSON backup of the selected entities to /config/orphan_cleaner_backups/orphan_cleaner_backup_<timestamp>.json before proceeding with deletion. If the backup fails, deletion is aborted. The backup contains entity metadata (entity_id, platform, detection method, age, disabled state) and can be used as a reference in case of accidental deletion.
Enable the Dry run checkbox in the toolbar before deleting. When active:
- An amber warning banner appears to remind you that no changes will be made
- Delete selected becomes Simulate
- Save & Delete is hidden
- Clicking Simulate highlights the selected entities with a
would deletebadge for 6 seconds and logs the result - nothing is actually removed
Dry run is also available as a parameter in the orphan_cleaner.delete_orphans service (see Services section).
After the first scan, every subsequent scan shows a diff banner above the table:
vs previous scan: 3 new ⬆ · 1 gone ⬇ · 108 unchanged
Entities that are new since the last scan are highlighted with a yellow new badge. The banner is hidden when results are identical to the previous scan.
Click any column header in the results table to sort by that field. Click again to reverse the order. The active sort direction is indicated by ↑ or ↓ next to the column name. Sortable columns: Entity ID, Platform, Category, Age.
After each scan, a compact bar appears below the stat cards:
59 safe · 36 to review · 4 only unavailable · 2 still used
A dropdown in the toolbar lets you show only one category: Safe to delete, To review or Only unavailable. Combine it with the text filter for precise results.
Entities still referenced by automations, scripts or scenes get a red used by N badge (hover it to see where). They are never selected by Select safe, and the confirmation dialog warns you if you selected them manually.
Click any platform badge in the results table to instantly select all entities of that platform. Useful for quickly selecting a full group before deleting or ignoring.
Every candidate gets a category, which tells how safe it is to delete it, and a short reason.
| Category | Meaning | Included in "delete all"? |
|---|---|---|
| Safe | Nothing provides the entity any more: its integration was removed, or Home Assistant restored it from the registry (state attribute restored: true, the yellow "no longer provided" warning) while its integration is running. |
Yes |
| To review | Its integration is disabled, failing or retrying setup, or it was found by the heuristic. It may come back on its own. | No |
| Only unavailable | Its integration still provides it, but it has been unavailable for at least Min age (h) (device off or unreachable). Deleting it only lasts until the next reload. |
No |
Detection steps, in order:
- Removed integration - the entity's config entry no longer exists → safe.
- Integration not running - the config entry is disabled, not loaded, in setup error, retrying setup or in migration error → to review.
- No longer provided - the state has
restored: true→ safe. Skipped during the first 10 minutes after Home Assistant starts, while integrations are still loading (the panel shows a notice). - Unavailable - unavailable for at least Min age (h), measured from the state's last change (not from when the entity was created) → only unavailable.
- Heuristic (optional, Heuristic checkbox) - no config entry, a platform not in the exclusion list and no current state → to review. Entities that currently have a state (for example YAML
utility_meterorhistory_statssensors) are still provided and are not reported.
Heuristic exclusion list: automation, counter, group, input_boolean, input_button, input_datetime, input_number, input_select, input_text, person, persistent_notification, scene, schedule, script, tag, template, timer, zone
Devices that have no entities left and whose integration no longer exists are listed separately and can be removed with one click, or with the orphan_cleaner.delete_orphan_devices service.
Runs the scan and fires the orphan_cleaner_orphans_found event on the HA event bus.
action: orphan_cleaner.scanEvent payload:
count: 5
count_safe: 3
startup_grace: false
orphans:
- entity_id: sensor.old_shelly
platform: shelly
method: restored
category: safe
reason: no longer provided by its integration
age_hours: null
disabled_by: null
used_in: []
orphan_devices:
- device_id: 0123abcd...
name: Old plugExample automation:
automation:
trigger:
platform: event
event_type: orphan_cleaner_orphans_found
condition:
condition: template
value_template: "{{ trigger.event.data.count > 0 }}"
action:
service: notify.mobile_app
data:
title: "Orphan entities detected"
message: "Found {{ trigger.event.data.count_safe }} orphan entities safe to delete."Deletes the specified entities or, if none are specified, only the candidates in the safe category.
| Field | Type | Required | Description |
|---|---|---|---|
entity_ids |
list | No | Entities to delete. If omitted → only the safe orphans |
dry_run |
bool | No | Report what would be deleted, without making changes (default: false) |
# Delete all safe orphans
action: orphan_cleaner.delete_orphans
# Dry run
action: orphan_cleaner.delete_orphans
data:
dry_run: true
# Delete specific entities
action: orphan_cleaner.delete_orphans
data:
entity_ids:
- sensor.old_device
- binary_sensor.ghost_sensorOn Home Assistant 2025.6 or later, all three services return their result as a service response (in Developer tools > Actions, tick Return response; in scripts and automations, use response_variable):
scan: the same data as theorphan_cleaner_orphans_foundevent.delete_orphansanddelete_orphan_devices: withdry_run: true,would_deletelists what would be deleted (withforce,detailsadds each device's entities and where they are still used); otherwisedeletedandfailed.
A dry run is also written to the log as a warning, so it shows up with the default log level.
action: orphan_cleaner.delete_orphans
data:
dry_run: true
response_variable: result
# result.would_delete -> ["sensor.old_device", ...]Removes devices with no entities left whose integration no longer exists. Without device_ids, removes all of them. Supports dry_run.
action: orphan_cleaner.delete_orphan_devices
data:
dry_run: trueSet force: true to remove specific devices from the device registry even when they are not detected as orphans. force requires device_ids, and the device's entities are removed with it.
force is for devices that their integration no longer provides but never removed from the registry, for example a device deleted from a cloud account while the integration is still installed. It does not help when the integration still knows the device:
- If the device still has its own entry under Settings > Devices & services (as ESPHome and Shelly devices do), the integration recreates it after a restart or reload, with the same id. Recent Home Assistant versions also restore its name and area. To remove such a device for good, delete its entry from the integration instead.
Use force with care:
- Entities still used by automations, scripts or scenes are not protected. The device, its entities and anything still using them are written to the log as warnings, so run with
dry_run: truefirst.
action: orphan_cleaner.delete_orphan_devices
data:
device_ids:
- 0123456789abcdef0123456789abcdef
force: truecustom_components/orphan_cleaner/
├── __init__.py ← setup, panel registration, teardown
├── config_flow.py ← initial config flow
├── const.py ← constants
├── manifest.json ← integration metadata
├── orphan_detector.py ← detection and deletion logic
├── panel_api.py ← HTTP views (panel, scan, delete, export, ignore list)
├── services.py ← HA services (scan, delete_orphans)
├── services.yaml ← service documentation for Developer Tools
├── strings.json ← UI labels
├── translations/
│ └── en.json
└── frontend/
├── panel.html ← full web interface
└── orphan-cleaner-panel.js
If you used Save & Delete, a backup file was written to /config/orphan_cleaner_backups/orphan_cleaner_backup_<timestamp>.json before the deletion. This file contains the metadata of all deleted entities.
{
"exported_at": "2024-11-15T10:32:00",
"entity_count": 3,
"entities": [
{
"entity_id": "sensor.old_device_temperature",
"platform": "tuya",
"method": "dead_entry",
"age_hours": 312.5,
"disabled_by": null
}
]
}- Check if the entity was truly orphaned - look at the
methodandage_hoursfields. Ifage_hoursis low, the integration may have been temporarily offline and the entity could reappear after a restart. - Reinstall the integration - if the platform (e.g.
tuya,shelly,zha) is still active and the physical device is reachable, removing and re-adding the integration will recreate its entities automatically. - Restore custom attributes manually - if the entity reappears, you can reassign its area, icon, and aliases from the HA UI. The backup gives you the original
entity_idto match it. - Clean up the recorder - if the entity had historical data and you want to remove it, use the
recorder.purge_entitiesservice with theentity_idfrom the backup.
- The entity registry entry itself - HA does not expose an API to re-insert entries manually.
- Historical recorder data is not deleted by this integration, but it will no longer be linked to an active entity.
- If the underlying integration or device no longer exists, the entity cannot be recreated without it.
All API endpoints require Home Assistant authentication. The panel itself requires admin privileges to access from the sidebar (require_admin). Endpoints that delete entities or write files to the configuration directory (/delete, /export) additionally require an admin-level account, regardless of how they are called. Read-only endpoints (/scan, /ignore_list GET) require a valid authenticated session but not admin rights.
All services (orphan_cleaner.scan, orphan_cleaner.delete_orphans, orphan_cleaner.delete_orphan_devices) are admin-only. A call made by a non-admin user is rejected, including one started from a dashboard button or a script that the user runs. Automations that run on their own triggers are not affected.
- Always make a backup before deleting entities in bulk. Use Save & Delete to automatically export a JSON record before each deletion.
- Deleted entities are removed from the registry: historical data in the recorder remains but is no longer associated with an active entity. To clean the recorder as well, use the
recorder.purge_entitiesservice. - Entities in the to review and only unavailable categories may come back after a reload or when their integration recovers: deleting them only loses their customisations (name, area, icon).
- Deletion is irreversible. Entities cannot be restored from the backup - the backup is a reference record only.
MIT