Skip to content

Commit 80ac0a0

Browse files
committed
feat(cleanup): Delete groups by ID
Instead of deleting by projects, let's attempt by using the ID.
1 parent 7b4f672 commit 80ac0a0

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/sentry/options/defaults.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@
341341
default=10000,
342342
flags=FLAG_AUTOMATOR_MODIFIABLE,
343343
)
344+
register(
345+
"cleanup.group-deletion-by-id",
346+
default=False,
347+
type=Bool,
348+
flags=FLAG_AUTOMATOR_MODIFIABLE,
349+
)
344350

345351
# Filestore (default)
346352
register("filestore.backend", default="filesystem", flags=FLAG_NOSTORE)

src/sentry/runner/commands/cleanup.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from django.db.models import Model, QuerySet
1818
from django.utils import timezone
1919

20+
from sentry import options
2021
from sentry.runner.decorators import log_options
2122
from 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

485492
def 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

Comments
 (0)