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(data-deletion): sweep flag_evaluations in immediate event removal (#114271)

Phil Haack437b7b857873
diff --git a/docs/internal/clickhouse-deletion-coverage.md b/docs/internal/clickhouse-deletion-coverage.md
index fd0d080b33a8..a686362a796e 100644
--- a/docs/internal/clickhouse-deletion-coverage.md
+++ b/docs/internal/clickhouse-deletion-coverage.md
@@ -95,7 +95,7 @@ Leaving one out stays possible, and `PERSON_ID_REWRITE_EXEMPT` is where that dec
- `sharded_events` — all sweeps.
- `sharded_events_json` — all sweeps, on the events cluster, through patch parts instead of mutations (`uses_patch_parts`). Optional: only present after the native-JSON migration.
-- `sharded_flag_evaluations` — person, team, queued-uuid, deferred event removal, and property removal of event `properties` (below). Not immediate event removal, and not property removal with a HogQL predicate. Optional.
+- `sharded_flag_evaluations` — person, team, queued-uuid, immediate and deferred event removal, and property removal of event `properties` (below). Neither event removal nor property removal sweeps it when the request carries a HogQL predicate. Optional.
- `sharded_posthog_document_embeddings_<model>` — event and team deletion, through `delete_event_documents`. An embedded document is keyed by the id of the thing it describes (`document_id`), and an Event deletion's key is that same id, so the pending dictionary is joined on `(team_id, Event, document_id)`. Every per-model table listed by the error tracking facade's `document_embedding_tables` is swept and counted.
Native property-removal requests fail when the selected rows retain a requested permanent or temporary property, or a matching person `$set`/`$set_once` instruction.
@@ -210,21 +210,23 @@ The copy carries every other column through unchanged.
`flag_evaluations` stays absent from `MATERIALIZATION_VALID_TABLES`.
The job finds typed columns through their comments in `system.columns`, not through that set, and adding the table there enrolls it in `materialize()` and the HogQL property planner, which is a separate change.
-### Immediate event removal skips `flag_evaluations`
+### Immediate event removal with a HogQL predicate skips `flag_evaluations`
-`get_event_removal_shards` leaves `flag_evaluations` out of its targets, so an immediate request neither sweeps the table nor checks it for matching rows.
+`get_event_removal_shards` leaves `flag_evaluations` out of an immediate request that carries a HogQL predicate, so the request neither sweeps the table nor checks it for matching rows.
The rows age out with the table's TTL. The table partitions by month with `ttl_only_drop_parts = 1`, so a part drops only once its newest row expires: the real wait is up to about 120 days, not the 90-day TTL.
-Deferred event removal still queues the table's uuids, and `deletes_job` removes them.
-The skip exists because of the HogQL gap below: before it, the gate refused every immediate request with a predicate whose team had matching `$feature_flag_called` rows.
+The skip exists because of the HogQL gap below: without it, the gate refuses every immediate request with a predicate whose team has matching `$feature_flag_called` rows.
+An immediate request without a predicate sweeps the table and verifies it like the events tables, because its delete predicate names only `team_id`, `timestamp` and `event`.
### Event removal with a HogQL predicate does not reach `flag_evaluations`
`compile_hogql_predicate` resolves every predicate against the events HogQL table and emits events-specific physical columns.
Its only axis of variation is legacy vs native-JSON events, so while the dag does compile one fragment per target, no target selects a different table root.
Whether a given fragment would run against `flag_evaluations` depends on the predicate and the team's modifiers: one naming only `event` or `distinct_id` would, one reaching a `mat_*` column or a property-group map would not, and nothing validates which.
-The dag refuses rather than guessing.
+The dag does not guess.
+It never runs a fragment against the table.
+A deferred request with a predicate is refused if the table holds matching rows, and an immediate one skips the table (above).
A HogQL table definition for `flag_evaluations` does not change that, because nothing routes compilation to a table.
-Deferred requests without a predicate are swept normally; deferred requests with one are refused if the table holds matching rows.
+Requests without a predicate, immediate or deferred, are swept normally.
## Producer prerequisite: person_id parity
diff --git a/posthog/dags/data_deletion_requests.py b/posthog/dags/data_deletion_requests.py
index 31e5cfda6ead..d106eb01b10f 100644
--- a/posthog/dags/data_deletion_requests.py
+++ b/posthog/dags/data_deletion_requests.py
@@ -664,10 +664,10 @@ def _event_removal_placements(
return [p for p in placements if p.target.accepts_hogql_predicate]
-# Immediate deletion leaves flag_evaluations rows to the table's TTL. A HogQL predicate does not
-# compile against that table, so gating on it refused every such request whose team had matching
-# $feature_flag_called rows. Deferred deletion still queues the table's uuids.
-_IMMEDIATE_SKIP_TARGETS = (FLAG_EVALUATIONS,)
+# An immediate request with a HogQL predicate leaves flag_evaluations rows to the table's TTL. The
+# predicate does not compile against that table. Without the skip, the unsweepable-rows gate would
+# refuse every such request whose team has matching $feature_flag_called rows.
+_IMMEDIATE_HOGQL_SKIP_TARGETS = (FLAG_EVALUATIONS,)
@frozen
@@ -794,7 +794,8 @@ def get_event_removal_shards(
if deletion_request.execution_mode == ExecutionMode.DEFERRED.value:
return
- placements = _event_removal_placements(cluster, deletion_request, skip_targets=_IMMEDIATE_SKIP_TARGETS)
+ skip_targets = _IMMEDIATE_HOGQL_SKIP_TARGETS if deletion_request.hogql_predicate else ()
+ placements = _event_removal_placements(cluster, deletion_request, skip_targets=skip_targets)
# placement.cluster, not the job's handle: shard numbers are per cluster.
shards = [
EventRemovalShard(data_table=placement.target.data_table, shard_num=shard_num)
diff --git a/posthog/dags/tests/test_data_deletion_requests.py b/posthog/dags/tests/test_data_deletion_requests.py
index a818d486055c..dac40d204529 100644
--- a/posthog/dags/tests/test_data_deletion_requests.py
+++ b/posthog/dags/tests/test_data_deletion_requests.py
@@ -2799,7 +2799,21 @@ def test_full_job_property_removal_survives_the_ttl_dropping_reingested_flag_eva
@pytest.mark.django_db
-def test_immediate_event_deletion_skips_flag_evaluations(cluster: ClickhouseCluster) -> None:
+ "events, delete_all_events, hogql_predicate, surviving_flag_evaluations",
+ [
+ pytest.param([FLAG_EVALUATIONS_SOURCE_EVENT], False, "", 0, id="named_event"),
+ pytest.param([], True, "", 0, id="all_events"),
+ pytest.param([FLAG_EVALUATIONS_SOURCE_EVENT], False, "properties.$browser = 'Chrome'", 1, id="hogql_predicate"),
+ ],
+)
+def test_immediate_event_deletion_of_flag_evaluations(
+ cluster: ClickhouseCluster,
+ events: list[str],
+ delete_all_events: bool,
+ hogql_predicate: str,
+ surviving_flag_evaluations: int,
+) -> None:
from posthog.models.organization import Organization
from posthog.models.team import Team
@@ -2826,13 +2840,14 @@ def test_immediate_event_deletion_skips_flag_evaluations(cluster: ClickhouseClus
team_id=team.id,
start_time=now - timedelta(days=7),
end_time=now + timedelta(minutes=1),
- events=[FLAG_EVALUATIONS_SOURCE_EVENT],
- hogql_predicate="properties.$browser = 'Chrome'",
+ events=events,
+ delete_all_events=delete_all_events,
+ hogql_predicate=hogql_predicate,
)
_run_event_deletion(cluster, deletion_ctx)
assert cluster.any_host(partial(_count_events_by_name, team.id, FLAG_EVALUATIONS_SOURCE_EVENT)).result() == 0
- assert len(cluster.any_host(partial(_flag_evaluation_person_ids, team.id)).result()) == 1
+ assert len(cluster.any_host(partial(_flag_evaluation_person_ids, team.id)).result()) == surviving_flag_evaluations
cluster.any_host(_truncate_flag_evaluations).result()
diff --git a/posthog/models/deletion_targets.py b/posthog/models/deletion_targets.py
index e94caf82b78e..af95317779d9 100644
--- a/posthog/models/deletion_targets.py
+++ b/posthog/models/deletion_targets.py
@@ -227,9 +227,11 @@ EVENTS_JSON = DeletionTarget(
# queued-uuid sweeps must reach it, and a person sweep matches on person_id like every other
# events-shaped table. Property removal rewrites its event properties. Its producer stopped sending
# person_properties on 2026-09-05 (#95693); a row the table stored before then is out of property
-# removal's reach until its TTL passes. A HogQL predicate does not compile against the table, which
-# limits immediate event removal and property removal there; both limits are explained in
-# docs/internal/clickhouse-deletion-coverage.md.
+# removal's reach until its TTL passes. A HogQL predicate does not compile against the table, so a
+# request that carries one never sweeps it. Immediate event removal with a predicate leaves the
+# matching rows to the TTL. Deferred event removal and property removal with a predicate refuse
+# while the table holds matching rows. docs/internal/clickhouse-deletion-coverage.md explains these
+# limits.
FLAG_EVALUATIONS = DeletionTarget(
data_table=FLAG_EVALUATIONS_DATA_TABLE,
read_table=FLAG_EVALUATIONS_TABLE,