Remove entity by automation

Hey,
I’d like to know if it’s possible to automate the removal of entities.
I have an app with which I manage my work schedule, with that I can add this schedule to my Google calendar which will be added as separate work calendar.
When I do changes to this and upload them to my Google Cal, it removes the old calendar and adds the new one.
In HA, the sync doesn’t work as flawless, the old work calendar entity becomes unavailable and a new one will be added as calendar.work_2.
Naturally that messes with automations and I’d like to remove the old entity automatically when it becomes unavailable and refresh the Google calendar service/rename the new entity.

Is that possible?

Not with only the built-in actions. Spook has a delete all orphaned entities action, but the question is whether this entity would be considered orphaned or not. Guess you’ll have to just try and see what happens… Spook also has en action to update an entity ID.

Thanks for the hint.

alias: Delete orphaned database entities
sequence:
  - action: homeassistant.list_orphaned_database_entities
    response_variable: orphaned
  - action: recorder.purge_entities
    target:
      entity_id: |
        {{ orphaned.entities }}
    data:
      keep_days: 0
mode: single

Maybe I could just use the recorder.purge_entities action and switch the entity id to the one I want? :thinking:

Edit: no, that would only delete the entities from the database of orphaned entities. :pleading_face:

I would start with fixing this instead of letting an automation to delete anything (if you find a way to do it).

1 Like