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): shrink team-delete batch size to clear personhog deadline storm (#67536)

Eli Reismanb9ed34121ed6
diff --git a/posthog/models/person/test/test_delete_personless_personhog.py b/posthog/models/person/test/test_delete_personless_personhog.py
index a7ac21d560b5..7bf02522043e 100644
--- a/posthog/models/person/test/test_delete_personless_personhog.py
+++ b/posthog/models/person/test/test_delete_personless_personhog.py
@@ -45,4 +45,4 @@ class TestDeletePersonlessDistinctIdsForTeamsRPC(BaseTest):
team_ids_called = {c.request.team_id for c in calls}
assert self.team.pk in team_ids_called
assert other_team.pk in team_ids_called
- assert all(c.request.batch_size == 10000 for c in calls)
+ assert all(c.request.batch_size == 2000 for c in calls)
diff --git a/posthog/models/team/test_util_deletion.py b/posthog/models/team/test_util_deletion.py
index bd034614aa9b..329327e7cd04 100644
--- a/posthog/models/team/test_util_deletion.py
+++ b/posthog/models/team/test_util_deletion.py
@@ -99,7 +99,7 @@ class TestDeleteHashKeyOverridesForTeams(SimpleTestCase):
assert mock_client.delete_hash_key_overrides_by_teams.call_count == 1
req = mock_client.delete_hash_key_overrides_by_teams.call_args[0][0]
assert list(req.team_ids) == [1, 2, 3]
- assert req.batch_size == 10000
+ assert req.batch_size == 2000
@patch(_CLIENT_PATCH)
def test_loops_until_zero_deleted(self, mock_get_client):
diff --git a/posthog/models/team/util.py b/posthog/models/team/util.py
index df50478e949b..ef0ef9441533 100644
--- a/posthog/models/team/util.py
+++ b/posthog/models/team/util.py
@@ -13,6 +13,13 @@ 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.
+TEAM_DELETE_BATCH_SIZE = 2000
+
actions_that_require_current_team = [
"rotate_secret_token",
"delete_secret_token_backup",
@@ -82,7 +89,7 @@ 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=10000)
+ DeleteHashKeyOverridesByTeamsRequest(team_ids=team_ids, batch_size=TEAM_DELETE_BATCH_SIZE)
)
if resp.deleted_count == 0:
break
@@ -115,7 +122,7 @@ 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=10000)
+ DeletePersonlessDistinctIdsBatchForTeamRequest(team_id=team_id, batch_size=TEAM_DELETE_BATCH_SIZE)
)
if resp.deleted_count == 0:
break
@@ -154,7 +161,9 @@ def _delete_persons_for_team_via_personhog(team_id: int) -> None:
client = require_personhog_client()
while True:
- resp = client.delete_persons_batch_for_team(DeletePersonsBatchForTeamRequest(team_id=team_id, batch_size=10000))
+ resp = client.delete_persons_batch_for_team(
+ DeletePersonsBatchForTeamRequest(team_id=team_id, batch_size=TEAM_DELETE_BATCH_SIZE)
+ )
if resp.deleted_count == 0:
break