: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-deletion): align verification with sweep targets (#108339)
Paweł Szczur9dd686c33f29
diff --git a/docs/internal/clickhouse-deletion-coverage.md b/docs/internal/clickhouse-deletion-coverage.mdindex 093d4e785af4..9143b96c8f50 100644--- a/docs/internal/clickhouse-deletion-coverage.md+++ b/docs/internal/clickhouse-deletion-coverage.md@@ -152,6 +152,8 @@ mutation an earlier run enqueued, which is the failure the count was added to no`SweepTargetsConfig.skip_targets` defaults to `["sharded_events_json"]`, so the weekly sweep leavesthat table alone and does not address the events cluster at all.+Deletion request verification reads the same `DEFAULT_DELETION_TARGETS` set, so it does not count+rows in a table that the default sweep leaves unchanged.The table's storage is on the events cluster, which `deletes_job` reaches through a sibling handle.Resolving that handle has not been reliable: a run that fails to resolve it drops the target and@@ -167,7 +169,7 @@ sweep does remove from `sharded_events`.To sweep it again:- For one run, set `skip_targets: []` under the `resolve_sweep_targets` op in run config.-- Permanently, remove the entry from `_DEFAULT_SKIP_TARGETS` in `posthog/dags/deletes.py`.+- Permanently, add `EVENTS_JSON` to `DEFAULT_DELETION_TARGETS` in `posthog/models/deletion_targets.py`.Neither restores what earlier runs left behind. That needs a backfill sweep over the affected uuids.diff --git a/posthog/dags/deletes.py b/posthog/dags/deletes.pyindex 348fe39eb26e..6b51ec25f231 100644--- a/posthog/dags/deletes.py+++ b/posthog/dags/deletes.py@@ -38,6 +38,7 @@ from posthog.dataclasses import frozenfrom posthog.models.async_deletion import AsyncDeletion, DeletionTypefrom posthog.models.deletion_targets import (COVERAGE_DOC,+ DEFAULT_DELETION_TARGETS,PERSONAL_DATA_TARGETS,DeletionTarget,_any_node_has,@@ -46,7 +47,7 @@ from posthog.models.deletion_targets import (sweep_clusters,)from posthog.models.event.deletion import events_data_tables-from posthog.models.event.sql import EVENTS_DATA_TABLE, EVENTS_JSON_DATA_TABLE+from posthog.models.event.sql import EVENTS_DATA_TABLEfrom posthog.models.group.sql import GROUPS_TABLEfrom posthog.models.person.sql import (PERSON_DISTINCT_ID2_TABLE,@@ -102,18 +103,14 @@ class DeleteConfig(dagster.Config):return datetime.fromisoformat(self.timestamp)-# sharded_events_json is skipped until the events cluster is reliably reachable from the sweep.-# A run that resolves it inconsistently is worse than one that never tries: it creates the-# dictionary on a cluster it may not mutate, and reports an erasure that did not happen. Rows the-# table holds stay readable meanwhile, which is the cost this accepts; see COVERAGE_DOC.-# Remove it from the default to sweep the table again. `skip_targets: []` in run config does the-# same for one run, without a deploy.-_DEFAULT_SKIP_TARGETS = [EVENTS_JSON_DATA_TABLE]--class SweepTargetsConfig(dagster.Config):+ # sharded_events_json is skipped until the events cluster is reliably reachable from the sweep.+ # A run that resolves it inconsistently can report an erasure without mutating its rows. Add it+ # to DEFAULT_DELETION_TARGETS to sweep and verify it again, or pass [] for one run.skip_targets: list[str] = pydantic.Field(- default_factory=lambda: list(_DEFAULT_SKIP_TARGETS),+ default_factory=lambda: [+ target.data_table for target in PERSONAL_DATA_TARGETS if target not in DEFAULT_DELETION_TARGETS+ ],description="Deletion targets to leave out of this run, named by either their storage or "'their read table, e.g. ["sharded_events_json"] or ["events_json"]. A skipped target gets '"no dictionary, no mutation and no survivor count, and a cluster only it lives on is not "diff --git a/posthog/dags/tests/test_deletes.py b/posthog/dags/tests/test_deletes.pyindex 265d27a71f86..b06acb337e50 100644--- a/posthog/dags/tests/test_deletes.py+++ b/posthog/dags/tests/test_deletes.py@@ -42,7 +42,7 @@ from posthog.dags.deletes import (from posthog.dags.person_overrides import squash_person_overridesfrom posthog.dags.tests.conftest import insert_flag_evaluationsfrom posthog.models.async_deletion import AsyncDeletion, DeletionType-from posthog.models.deletion_targets import EVENTS, PERSONAL_DATA_TARGETS, TargetPlacement+from posthog.models.deletion_targets import DEFAULT_DELETION_TARGETS, EVENTS, PERSONAL_DATA_TARGETS, TargetPlacementfrom posthog.models.event.sql import EVENTS_DATA_TABLEfrom posthog.models.person.sql import PERSON_DISTINCT_ID_OVERRIDES_TABLE@@ -1219,7 +1219,9 @@ def test_events_json_is_skipped_by_default() -> None:# The events cluster is not reliably reachable from the sweep, and a run that resolves it# inconsistently reports an erasure it did not perform. Dropping the default would resume that# sweep silently, because nothing else in a run says which targets it was supposed to reach.- assert resolve_sweep_targets(build_op_context()) == ["sharded_events", "sharded_flag_evaluations"]+ expected = ["sharded_events", "sharded_flag_evaluations"]+ assert resolve_sweep_targets(build_op_context()) == expected+ assert [target.data_table for target in DEFAULT_DELETION_TARGETS] == expecteddef test_an_unrecognised_skip_target_fails_the_run() -> None:diff --git a/posthog/models/data_deletion_request.py b/posthog/models/data_deletion_request.pyindex be66f3cfbc2d..456e5dae5c89 100644--- a/posthog/models/data_deletion_request.py+++ b/posthog/models/data_deletion_request.py@@ -814,8 +814,8 @@ def refresh_deletion_stats(request: "DataDeletionRequest", *, user_id: int | Nondef count_remaining_matching_events(request: "DataDeletionRequest") -> int:"""Count rows still matching an event-removal request's criteria in ClickHouse.- Counts across every registered read table that could hold the named events. A request is only- complete once its rows are gone from all of them.+ Counts across every default deletion target that could hold the named events. A request is only+ complete once its rows are gone from every table that the scheduled deletion job sweeps.A target that cannot take the compiled HogQL fragment is counted with the portable predicateinstead, which matches a superset. That can only hold a request in QUEUED, never promote one@@ -826,7 +826,11 @@ def count_remaining_matching_events(request: "DataDeletionRequest") -> int:from posthog.clickhouse.client.connection import ClickHouseUserfrom posthog.clickhouse.query_tagging import Feature, Product, tags_contextfrom posthog.clickhouse.workload import Workload- from posthog.models.deletion_targets import resolve_read_targets_via_sync_execute, surviving_rows_sql+ from posthog.models.deletion_targets import (+ DEFAULT_DELETION_TARGETS,+ resolve_read_targets_via_sync_execute,+ surviving_rows_sql,+ )events = [] if request.delete_all_events else request.eventstotal = 0@@ -837,7 +841,7 @@ def count_remaining_matching_events(request: "DataDeletionRequest") -> int:workload=Workload.OFFLINE,query_type="data_deletion_request_verify_queued",):- for target in resolve_read_targets_via_sync_execute():+ for target in resolve_read_targets_via_sync_execute(DEFAULT_DELETION_TARGETS):if not target.may_hold_any_of(events):continueif target.accepts_hogql_predicate:diff --git a/posthog/models/deletion_targets.py b/posthog/models/deletion_targets.pyindex 5bcb3d4f52e7..e8443974df21 100644--- a/posthog/models/deletion_targets.py+++ b/posthog/models/deletion_targets.py@@ -211,6 +211,13 @@ FLAG_EVALUATIONS = DeletionTarget(EVENTS_TARGETS: tuple[DeletionTarget, ...] = (EVENTS, EVENTS_JSON)PERSONAL_DATA_TARGETS: tuple[DeletionTarget, ...] = (*EVENTS_TARGETS, FLAG_EVALUATIONS)+# sharded_events_json stays registered because deletion support will return when the events cluster+# is reliably reachable. Keeping the default sweep targets separate makes every verifier follow the+# same temporary exclusion as deletes_job.+DEFAULT_DELETION_TARGETS: tuple[DeletionTarget, ...] = tuple(+ target for target in PERSONAL_DATA_TARGETS if target is not EVENTS_JSON+)+# Every table squash_person_overrides rewrites person_id on. Derived from the capability rather than# listed by hand, so registering a target and forgetting the squash is not expressible.SQUASH_TARGETS: tuple[DeletionTarget, ...] = tuple(