From a4653bb8dcbbcbd1bb8c731044c65cfa563fc59b Mon Sep 17 00:00:00 2001
From: Erik <erik@montnemery.com>
Date: Mon, 2 Dec 2024 15:22:23 +0100
Subject: [PATCH] Clean up registries when removing subentry

---
 homeassistant/config_entries.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py
index d34828f5e46..320083386c1 100644
--- a/homeassistant/config_entries.py
+++ b/homeassistant/config_entries.py
@@ -2409,7 +2409,13 @@ class ConfigEntries:
         except KeyError as err:
             raise UnknownSubEntry from err
 
-        return self._async_update_entry(entry, subentries=subentries)
+        result = self._async_update_entry(entry, subentries=subentries)
+        dev_reg = dr.async_get(self.hass)
+        ent_reg = er.async_get(self.hass)
+
+        dev_reg.async_clear_config_subentry(entry.entry_id, subentry_id)
+        ent_reg.async_clear_config_subentry(entry.entry_id, subentry_id)
+        return result
 
     def _raise_if_subentry_unique_id_exists(
         self, entry: ConfigEntry, unique_id: str | None