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: prevent file-system view log from blocking org deletion (#57940)

Alex Lideread42d74d12a
diff --git a/posthog/models/team/util.py b/posthog/models/team/util.py
index cf1c8762a88d..c73f9f321059 100644
--- a/posthog/models/team/util.py
+++ b/posthog/models/team/util.py
@@ -27,6 +27,7 @@ def delete_bulky_postgres_data(team_ids: list[int]):
from posthog.models.cohort import Cohort
from posthog.models.feature_flag.feature_flag import FeatureFlagHashKeyOverride
+ from posthog.models.file_system.file_system_view_log import FileSystemViewLog
from posthog.models.insight_caching_state import InsightCachingState
from posthog.models.person import PersonlessDistinctId
@@ -39,6 +40,8 @@ def delete_bulky_postgres_data(team_ids: list[int]):
_raw_delete(Edge.objects.filter(team_id__in=team_ids))
_raw_delete(Node.objects.filter(team_id__in=team_ids))
+ _raw_delete(FileSystemViewLog.objects.filter(team_id__in=team_ids))
+
_raw_delete(EarlyAccessFeature.objects.filter(team_id__in=team_ids))
_raw_delete_batch(PersonlessDistinctId.objects.filter(team_id__in=team_ids)) # nosemgrep: no-direct-persons-db-orm
_raw_delete(ErrorTrackingIssueFingerprintV2.objects.filter(team_id__in=team_ids))
diff --git a/posthog/tasks/tasks.py b/posthog/tasks/tasks.py
index 5198f177a3ea..f5df2ef4fd19 100644
--- a/posthog/tasks/tasks.py
+++ b/posthog/tasks/tasks.py
@@ -7,7 +7,7 @@ if TYPE_CHECKING:
from uuid import UUID
from django.conf import settings
-from django.db import connection
+from django.db import connection, transaction
from django.utils import timezone
import requests
@@ -1080,10 +1080,13 @@ def _delete_teams_and_data(team_ids: list[int], user_id: int, project_id: int |
delete_data_modeling_schedules(team_ids=team_ids)
logger.info("Deleting team records", team_ids=team_ids)
- if project_id:
- Project.objects.filter(id=project_id).delete()
- else:
- Team.objects.filter(id__in=team_ids).delete()
+ # FOR UPDATE on teams blocks concurrent FK-inserts to any child table during cascade.
+ with transaction.atomic():
+ list(Team.objects.select_for_update().filter(id__in=team_ids))
+ if project_id:
+ Project.objects.filter(id=project_id).delete()
+ else:
+ Team.objects.filter(id__in=team_ids).delete()
logger.info("Queueing ClickHouse deletion", team_ids=team_ids)
AsyncDeletion.objects.bulk_create(