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