@@ -2181,6 +2181,143 @@ def _find_unreferenced_groups_during_purge(self, txn, state_groups):
2181
2181
2182
2182
return to_delete , to_dedelta
2183
2183
2184
+ def purge_room (self , room_id ):
2185
+ """Deletes all record of a room
2186
+
2187
+ Args:
2188
+ room_id (str):
2189
+ """
2190
+
2191
+ return self .runInteraction ("purge_room" , self ._purge_room_txn , room_id )
2192
+
2193
+ def _purge_room_txn (self , txn , room_id ):
2194
+ # first we have to delete the state groups states
2195
+ logger .info ("[purge] removing %s from state_groups_state" , room_id )
2196
+
2197
+ txn .execute (
2198
+ """
2199
+ DELETE FROM state_groups_state WHERE state_group IN (
2200
+ SELECT state_group FROM events JOIN event_to_state_groups USING(event_id)
2201
+ WHERE events.room_id=?
2202
+ )
2203
+ """ ,
2204
+ (room_id ,),
2205
+ )
2206
+
2207
+ # ... and the state group edges
2208
+ logger .info ("[purge] removing %s from state_group_edges" , room_id )
2209
+
2210
+ txn .execute (
2211
+ """
2212
+ DELETE FROM state_group_edges WHERE state_group IN (
2213
+ SELECT state_group FROM events JOIN event_to_state_groups USING(event_id)
2214
+ WHERE events.room_id=?
2215
+ )
2216
+ """ ,
2217
+ (room_id ,),
2218
+ )
2219
+
2220
+ # ... and the state groups
2221
+ logger .info ("[purge] removing %s from state_groups" , room_id )
2222
+
2223
+ txn .execute (
2224
+ """
2225
+ DELETE FROM state_groups WHERE id IN (
2226
+ SELECT state_group FROM events JOIN event_to_state_groups USING(event_id)
2227
+ WHERE events.room_id=?
2228
+ )
2229
+ """ ,
2230
+ (room_id ,),
2231
+ )
2232
+
2233
+ # and then tables which lack an index on room_id but have one on event_id
2234
+ for table in (
2235
+ "event_auth" ,
2236
+ "event_edges" ,
2237
+ "event_push_actions_staging" ,
2238
+ "event_reference_hashes" ,
2239
+ "event_relations" ,
2240
+ "event_to_state_groups" ,
2241
+ "redactions" ,
2242
+ "rejections" ,
2243
+ "state_events" ,
2244
+ ):
2245
+ logger .info ("[purge] removing %s from %s" , room_id , table )
2246
+
2247
+ txn .execute (
2248
+ """
2249
+ DELETE FROM %s WHERE event_id IN (
2250
+ SELECT event_id FROM events WHERE room_id=?
2251
+ )
2252
+ """
2253
+ % (table ,),
2254
+ (room_id ,),
2255
+ )
2256
+
2257
+ # and finally, the tables with an index on room_id (or no useful index)
2258
+ for table in (
2259
+ "current_state_events" ,
2260
+ "event_backward_extremities" ,
2261
+ "event_forward_extremities" ,
2262
+ "event_json" ,
2263
+ "event_push_actions" ,
2264
+ "event_search" ,
2265
+ "events" ,
2266
+ "group_rooms" ,
2267
+ "public_room_list_stream" ,
2268
+ "receipts_graph" ,
2269
+ "receipts_linearized" ,
2270
+ "room_aliases" ,
2271
+ "room_depth" ,
2272
+ "room_memberships" ,
2273
+ "room_state" ,
2274
+ "room_stats" ,
2275
+ "room_stats_earliest_token" ,
2276
+ "rooms" ,
2277
+ "stream_ordering_to_exterm" ,
2278
+ "topics" ,
2279
+ "users_in_public_rooms" ,
2280
+ "users_who_share_private_rooms" ,
2281
+ # no useful index, but let's clear them anyway
2282
+ "appservice_room_list" ,
2283
+ "e2e_room_keys" ,
2284
+ "event_push_summary" ,
2285
+ "pusher_throttle" ,
2286
+ "group_summary_rooms" ,
2287
+ "local_invites" ,
2288
+ "room_account_data" ,
2289
+ "room_tags" ,
2290
+ ):
2291
+ logger .info ("[purge] removing %s from %s" , room_id , table )
2292
+ txn .execute ("DELETE FROM %s WHERE room_id=?" % (table ,), (room_id ,))
2293
+
2294
+ # Other tables we do NOT need to clear out:
2295
+ #
2296
+ # - blocked_rooms
2297
+ # This is important, to make sure that we don't accidentally rejoin a blocked
2298
+ # room after it was purged
2299
+ #
2300
+ # - user_directory
2301
+ # This has a room_id column, but it is unused
2302
+ #
2303
+
2304
+ # Other tables that we might want to consider clearing out include:
2305
+ #
2306
+ # - event_reports
2307
+ # Given that these are intended for abuse management my initial
2308
+ # inclination is to leave them in place.
2309
+ #
2310
+ # - current_state_delta_stream
2311
+ # - ex_outlier_stream
2312
+ # - room_tags_revisions
2313
+ # The problem with these is that they are largeish and there is no room_id
2314
+ # index on them. In any case we should be clearing out 'stream' tables
2315
+ # periodically anyway (#5888)
2316
+
2317
+ # TODO: we could probably usefully do a bunch of cache invalidation here
2318
+
2319
+ logger .info ("[purge] done" )
2320
+
2184
2321
@defer .inlineCallbacks
2185
2322
def is_event_after (self , event_id1 , event_id2 ):
2186
2323
"""Returns True if event_id1 is after event_id2 in the stream
0 commit comments