diff --git a/changelog.d/18017.misc b/changelog.d/18017.misc
new file mode 100644
index 0000000000000000000000000000000000000000..6b943a5bed213c291105a315f0f46db6f367ca53
--- /dev/null
+++ b/changelog.d/18017.misc
@@ -0,0 +1 @@
+Disable DB statement timeout when doing a purge room since it can be quite long.
diff --git a/synapse/storage/databases/main/purge_events.py b/synapse/storage/databases/main/purge_events.py
index 08244153a39114daf0a2b30360bb868c73a43218..c195685af8338df80637d4853c86a5072cd3ba93 100644
--- a/synapse/storage/databases/main/purge_events.py
+++ b/synapse/storage/databases/main/purge_events.py
@@ -376,6 +376,11 @@ class PurgeEventsStore(StateGroupWorkerStore, CacheInvalidationWorkerStore):
                 (room_id,),
             )
 
+        if isinstance(self.database_engine, PostgresEngine):
+            # Disable statement timeouts for this transaction; purging rooms can
+            # take a while!
+            txn.execute("SET LOCAL statement_timeout = 0")
+
         # First, fetch all the state groups that should be deleted, before
         # we delete that information.
         txn.execute(