From ecad88f5c5787f6a8b7e72d3cb97b868fb685484 Mon Sep 17 00:00:00 2001
From: Devon Hudson <devon.dmytro@gmail.com>
Date: Tue, 18 Feb 2025 14:44:38 +0000
Subject: [PATCH] Cleanup deleted state group references (#18165)

### Pull Request Checklist

<!-- Please read
https://element-hq.github.io/synapse/latest/development/contributing_guide.html
before submitting your pull request -->

* [x] Pull request is based on the develop branch
* [x] Pull request includes a [changelog
file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog).
The entry should:
- Be a short description of your change which makes sense to users.
"Fixed a bug that prevented receiving messages from other servers."
instead of "Moved X method from `EventStore` to `EventWorkerStore`.".
  - Use markdown where necessary, mostly for `code blocks`.
  - End with either a period (.) or an exclamation mark (!).
  - Start with a capital letter.
- Feel free to credit yourself, by adding a sentence "Contributed by
@github_username." or "Contributed by [Your Name]." to the end of the
entry.
* [x] [Code
style](https://element-hq.github.io/synapse/latest/code_style.html) is
correct
(run the
[linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))
---
 changelog.d/18165.bugfix                 |  1 +
 synapse/storage/databases/state/store.py |  8 ++++++
 tests/storage/test_purge.py              | 35 +++++++++++++++++++++++-
 3 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 changelog.d/18165.bugfix

diff --git a/changelog.d/18165.bugfix b/changelog.d/18165.bugfix
new file mode 100644
index 0000000000..eba5face52
--- /dev/null
+++ b/changelog.d/18165.bugfix
@@ -0,0 +1 @@
+Cleanup deleted state group references.
diff --git a/synapse/storage/databases/state/store.py b/synapse/storage/databases/state/store.py
index 8c7980e719..90d7beb92f 100644
--- a/synapse/storage/databases/state/store.py
+++ b/synapse/storage/databases/state/store.py
@@ -828,10 +828,18 @@ class StateGroupDataStore(StateBackgroundUpdateStore, SQLBaseStore):
             "DELETE FROM state_groups_state WHERE state_group = ?",
             [(sg,) for sg in state_groups_to_delete],
         )
+        txn.execute_batch(
+            "DELETE FROM state_group_edges WHERE state_group = ?",
+            [(sg,) for sg in state_groups_to_delete],
+        )
         txn.execute_batch(
             "DELETE FROM state_groups WHERE id = ?",
             [(sg,) for sg in state_groups_to_delete],
         )
+        txn.execute_batch(
+            "DELETE FROM state_groups_pending_deletion WHERE state_group = ?",
+            [(sg,) for sg in state_groups_to_delete],
+        )
 
         return True
 
diff --git a/tests/storage/test_purge.py b/tests/storage/test_purge.py
index 5d6a8518c0..916e42e731 100644
--- a/tests/storage/test_purge.py
+++ b/tests/storage/test_purge.py
@@ -247,7 +247,7 @@ class PurgeTests(HomeserverTestCase):
             1 + self.state_deletion_store.DELAY_BEFORE_DELETION_MS / 1000
         )
 
-        # We expect that the unreferenced state group has been deleted.
+        # We expect that the unreferenced state group has been deleted from all tables.
         row = self.get_success(
             self.state_store.db_pool.simple_select_one_onecol(
                 table="state_groups",
@@ -259,6 +259,39 @@ class PurgeTests(HomeserverTestCase):
         )
         self.assertIsNone(row)
 
+        row = self.get_success(
+            self.state_store.db_pool.simple_select_one_onecol(
+                table="state_groups_state",
+                keyvalues={"state_group": unreferenced_state_group},
+                retcol="state_group",
+                allow_none=True,
+                desc="test_purge_unreferenced_state_group",
+            )
+        )
+        self.assertIsNone(row)
+
+        row = self.get_success(
+            self.state_store.db_pool.simple_select_one_onecol(
+                table="state_group_edges",
+                keyvalues={"state_group": unreferenced_state_group},
+                retcol="state_group",
+                allow_none=True,
+                desc="test_purge_unreferenced_state_group",
+            )
+        )
+        self.assertIsNone(row)
+
+        row = self.get_success(
+            self.state_store.db_pool.simple_select_one_onecol(
+                table="state_groups_pending_deletion",
+                keyvalues={"state_group": unreferenced_state_group},
+                retcol="state_group",
+                allow_none=True,
+                desc="test_purge_unreferenced_state_group",
+            )
+        )
+        self.assertIsNone(row)
+
         # We expect there to now only be one state group for the room, which is
         # the state group of the last event (as the only outlier).
         state_groups = self.get_success(
-- 
GitLab