Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(persons): give team-deletion bulk deletes a generous gRPC deadline (#70473)

Yasen3a25aeca9115
diff --git a/posthog/models/team/util.py b/posthog/models/team/util.py
index b9b4a1f7ea39..1bc3fb18a15b 100644
--- a/posthog/models/team/util.py
+++ b/posthog/models/team/util.py
@@ -16,12 +16,27 @@ from products.batch_exports.backend.service import BatchExportServiceScheduleNot
logger = structlog.get_logger(__name__)
# Batch size for the personhog batch-delete RPCs on the largest team-scoped tables
-# (personless distinct IDs, persons, hash-key-overrides). Kept well below 10000 so each
-# DELETE commits comfortably inside the personhog gRPC deadline on multi-billion-row
-# tables; otherwise a batch that overruns the deadline is rolled back wholesale and the
-# activity retries with zero forward progress.
+# (personless distinct IDs, persons, hash-key-overrides). Kept well below 10000 to bound
+# how much work a single DELETE holds locks for.
TEAM_DELETE_BATCH_SIZE = 2000
+# Per-call gRPC deadline for the team-deletion bulk-delete RPCs. Their default is the 5s
+# client deadline meant for point lookups, but a single batch DELETE against a
+# multi-billion-row, delete-churned table (personless distinct IDs, persons, groups,
+# hash-key-overrides) can transiently take seconds when autovacuum lags — enough to blow a
+# 5s deadline. A DEADLINE_EXCEEDED there fails the whole activity, which then retries the
+# batched loop from scratch, so each batch gets a much more generous deadline.
+#
+# This bounds a *single* batch, not the whole loop — the enclosing Temporal activity's
+# start_to_close (HEAVY_ACTIVITY_TIMEOUT, 2h in posthog/temporal/delete_teams/workflows.py)
+# is the real bound on the full team deletion. Keep this comfortably below that: a wedged
+# single batch then surfaces DEADLINE_EXCEEDED with plenty of activity budget left for a
+# clean retry, and the per-call deadline never coincides with the activity's own timeout
+# (which would let a retry start while the previous DELETE is still running in personhog).
+# 30 min is orders of magnitude above any healthy single-batch DELETE yet 4x under the 2h
+# activity bound.
+TEAM_DELETE_RPC_TIMEOUT_SECONDS = 30 * 60
+
actions_that_require_current_team = [
"rotate_secret_token",
"delete_secret_token_backup",
@@ -108,7 +123,8 @@ def _delete_hash_key_overrides_for_teams(team_ids: list[int]) -> None:
def _fn() -> None:
while True:
resp = client.delete_hash_key_overrides_by_teams(
- DeleteHashKeyOverridesByTeamsRequest(team_ids=team_ids, batch_size=TEAM_DELETE_BATCH_SIZE)
+ DeleteHashKeyOverridesByTeamsRequest(team_ids=team_ids, batch_size=TEAM_DELETE_BATCH_SIZE),
+ timeout=TEAM_DELETE_RPC_TIMEOUT_SECONDS,
)
if resp.deleted_count == 0:
break
@@ -141,7 +157,8 @@ def _delete_personless_distinct_ids_for_team_via_personhog(team_id: int) -> None
while True:
resp = client.delete_personless_distinct_ids_batch_for_team(
- DeletePersonlessDistinctIdsBatchForTeamRequest(team_id=team_id, batch_size=TEAM_DELETE_BATCH_SIZE)
+ DeletePersonlessDistinctIdsBatchForTeamRequest(team_id=team_id, batch_size=TEAM_DELETE_BATCH_SIZE),
+ timeout=TEAM_DELETE_RPC_TIMEOUT_SECONDS,
)
if resp.deleted_count == 0:
break
@@ -181,7 +198,8 @@ def _delete_persons_for_team_via_personhog(team_id: int) -> None:
while True:
resp = client.delete_persons_batch_for_team(
- DeletePersonsBatchForTeamRequest(team_id=team_id, batch_size=TEAM_DELETE_BATCH_SIZE)
+ DeletePersonsBatchForTeamRequest(team_id=team_id, batch_size=TEAM_DELETE_BATCH_SIZE),
+ timeout=TEAM_DELETE_RPC_TIMEOUT_SECONDS,
)
if resp.deleted_count == 0:
break
@@ -198,7 +216,8 @@ def _delete_groups_for_teams(team_ids: list[int]) -> None:
def _fn(tid: int = team_id) -> None:
while True:
resp = client.delete_groups_batch_for_team(
- DeleteGroupsBatchForTeamRequest(team_id=tid, batch_size=10000)
+ DeleteGroupsBatchForTeamRequest(team_id=tid, batch_size=10000),
+ timeout=TEAM_DELETE_RPC_TIMEOUT_SECONDS,
)
if resp.deleted_count == 0:
break
@@ -217,7 +236,8 @@ def _delete_group_type_mappings_for_teams(team_ids: list[int]) -> None:
def _fn(tid: int = team_id) -> None:
while True:
resp = client.delete_group_type_mappings_batch_for_team(
- DeleteGroupTypeMappingsBatchForTeamRequest(team_id=tid, batch_size=10000)
+ DeleteGroupTypeMappingsBatchForTeamRequest(team_id=tid, batch_size=10000),
+ timeout=TEAM_DELETE_RPC_TIMEOUT_SECONDS,
)
if resp.deleted_count == 0:
break
@@ -236,7 +256,7 @@ def _delete_cohort_members_for_teams(team_ids: list[int], cohort_ids: list[int])
for team_id in team_ids:
team_cohort_ids = list(Cohort.objects.filter(team_id=team_id, id__in=cohort_ids).values_list("id", flat=True))
if team_cohort_ids:
- delete_cohort_members_bulk(team_id, team_cohort_ids)
+ delete_cohort_members_bulk(team_id, team_cohort_ids, timeout=TEAM_DELETE_RPC_TIMEOUT_SECONDS)
def _raw_delete_batch(queryset: Any, batch_size: int = 10000):
diff --git a/products/cohorts/backend/models/util.py b/products/cohorts/backend/models/util.py
index aaefd7bf118e..7b79b117e67d 100644
--- a/products/cohorts/backend/models/util.py
+++ b/products/cohorts/backend/models/util.py
@@ -1351,7 +1351,9 @@ def delete_cohort_member(team_id: int, cohort_id: int, person_id: int) -> bool:
_DELETE_BULK_MAX_ITERATIONS = 10_000
-def _delete_cohort_members_bulk_via_personhog(cohort_ids: list[int], batch_size: int) -> int:
+def _delete_cohort_members_bulk_via_personhog(
+ cohort_ids: list[int], batch_size: int, timeout: float | None = None
+) -> int:
from posthog.personhog_client.client import get_personhog_client
from posthog.personhog_client.proto import DeleteCohortMembersBulkRequest
@@ -1362,7 +1364,8 @@ def _delete_cohort_members_bulk_via_personhog(cohort_ids: list[int], batch_size:
total_deleted = 0
for _ in range(_DELETE_BULK_MAX_ITERATIONS):
resp = client.delete_cohort_members_bulk(
- DeleteCohortMembersBulkRequest(cohort_ids=cohort_ids, batch_size=batch_size)
+ DeleteCohortMembersBulkRequest(cohort_ids=cohort_ids, batch_size=batch_size),
+ timeout=timeout,
)
total_deleted += resp.deleted_count
if resp.deleted_count < batch_size:
@@ -1377,7 +1380,9 @@ def _delete_cohort_members_bulk_via_personhog(cohort_ids: list[int], batch_size:
return total_deleted
-def delete_cohort_members_bulk(team_id: int, cohort_ids: list[int], batch_size: int = 10_000) -> int:
+def delete_cohort_members_bulk(
+ team_id: int, cohort_ids: list[int], batch_size: int = 10_000, timeout: float | None = None
+) -> int:
"""Delete all cohort membership rows for the given cohort IDs via personhog.
Returns the total number of deleted rows.
@@ -1389,7 +1394,7 @@ def delete_cohort_members_bulk(team_id: int, cohort_ids: list[int], batch_size:
return personhog_call(
"delete_cohort_members_bulk",
- lambda: _delete_cohort_members_bulk_via_personhog(cohort_ids, batch_size),
+ lambda: _delete_cohort_members_bulk_via_personhog(cohort_ids, batch_size, timeout),
)