Removing orphaned entities from browser_mod

There are several topic entries here concerning the removal of orphaned entities, particularly from the Browser_mod integration. Instead of replying to each posting, I am creating a new post and hope if anyone is still looking for a solution, this will be helpful. I came across a discussion yesterday which proved useful. There was a link to Ghost which people have tried with mostly failed attempts. However on that page there is code for a script that actually uses a Ghost Action so you will need to install Ghost, I think. I have pasted the script below. Some that had success with the script have stated they needed to run it many times to remove all of the orphans. With this in mind, I suggest modifying this script by setting mode to restart. It will run until no more orphans exist and then fail when the orphan list is empty.

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

Also posting here is case anyone is looking for removing Browser Mod entities. Browser Mod now has a service browser_mod.deregister_browser which has include and exclude options meaning you can tidy up Browser Mod with one service call.