1717from django .db .models import Model , QuerySet
1818from django .utils import timezone
1919
20+ from sentry import options
2021from sentry .runner .decorators import log_options
2122from sentry .silo .base import SiloLimit , SiloMode
2223
@@ -459,6 +460,7 @@ def models_which_use_deletions_code_path() -> list[tuple[type[Model], str, str]]
459460 from sentry .models .commit import Commit
460461 from sentry .models .eventattachment import EventAttachment
461462 from sentry .models .files .file import File
463+ from sentry .models .group import Group
462464 from sentry .models .grouprulestatus import GroupRuleStatus
463465 from sentry .models .pullrequest import PullRequest
464466 from sentry .models .release import Release
@@ -468,7 +470,7 @@ def models_which_use_deletions_code_path() -> list[tuple[type[Model], str, str]]
468470
469471 # Deletions that use the `deletions` code path (which handles their child relations)
470472 # (model, datetime_field, order_by)
471- return [
473+ models = [
472474 (EventAttachment , "date_added" , "date_added" ),
473475 (ReplayRecordingSegment , "date_added" , "date_added" ),
474476 (ArtifactBundle , "date_added" , "date_added" ),
@@ -480,6 +482,11 @@ def models_which_use_deletions_code_path() -> list[tuple[type[Model], str, str]]
480482 (File , "timestamp" , "timestamp" ),
481483 (Commit , "date_added" , "id" ),
482484 ]
485+ if options .get ("cleanup.group-deletion-by-id" ):
486+ # Group deletion: filter by last_seen, but order by id to use the primary key index
487+ models .append ((Group , "last_seen" , "id" ))
488+
489+ return models
483490
484491
485492def remove_cross_project_models (
@@ -707,13 +714,14 @@ def prepare_deletes_by_project(
707714
708715 # Deletions that we run per project. In some cases we can't use an index on just the date
709716 # column, so as an alternative we use `(project_id, <date_col>)` instead
710- DELETES_BY_PROJECT = [
717+ DELETES_BY_PROJECT = [(ProjectDebugFile , "date_accessed" , "date_accessed" )]
718+
719+ if not options .get ("cleanup.group-deletion-by-id" ):
711720 # Having an index on `last_seen` sometimes caused the planner to make a bad plan that
712721 # used this index instead of a more appropriate one. This was causing a lot of postgres
713722 # load, so we had to remove it.
714- (Group , "last_seen" , "last_seen" ),
715- (ProjectDebugFile , "date_accessed" , "date_accessed" ),
716- ]
723+ DELETES_BY_PROJECT .insert (0 , (Group , "last_seen" , "id" ))
724+
717725 project_deletion_query = None
718726 to_delete_by_project = []
719727 if SiloMode .get_current_mode () != SiloMode .CONTROL :
0 commit comments