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(annotations): hide annotations on soft-deleted insights and dashboards (#58965)

Vasco de Krijger88b69218ba3f
diff --git a/posthog/api/annotation.py b/posthog/api/annotation.py
index f43eca619e02..9ff3ce799ec2 100644
--- a/posthog/api/annotation.py
+++ b/posthog/api/annotation.py
@@ -148,6 +148,15 @@ class AnnotationsViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, viewsets.Mo
# We never want deleted items to be included in the queryset… except when we want to restore an annotation
# That's because annotations are restored with a PATCH request setting `deleted` to `False`
queryset = queryset.filter(deleted=False)
+ # Annotations attached to a soft-deleted insight or dashboard are hidden
+ # across all actions — including `partial_update`, so they cannot be
+ # individually edited or restored while their parent is soft-deleted.
+ # They reappear automatically when the parent is restored. Mirrors how
+ # alerts behave (see posthog/tasks/alerts/checks.py).
+ queryset = queryset.filter(
+ Q(dashboard_item__isnull=True) | Q(dashboard_item__deleted=False),
+ Q(dashboard__isnull=True) | Q(dashboard__deleted=False),
+ )
scope = self.request.query_params.get("scope")
if scope:
diff --git a/posthog/api/test/__snapshots__/test_annotation.ambr b/posthog/api/test/__snapshots__/test_annotation.ambr
index c09af98b79c3..bc3076f994e3 100644
--- a/posthog/api/test/__snapshots__/test_annotation.ambr
+++ b/posthog/api/test/__snapshots__/test_annotation.ambr
@@ -363,7 +363,13 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
+ LEFT OUTER JOIN "posthog_dashboarditem" ON ("posthog_annotation"."dashboard_item_id" = "posthog_dashboarditem"."id")
+ LEFT OUTER JOIN "posthog_dashboard" ON ("posthog_annotation"."dashboard_id" = "posthog_dashboard"."id")
WHERE (NOT "posthog_annotation"."deleted"
+ AND ("posthog_annotation"."dashboard_item_id" IS NULL
+ OR NOT "posthog_dashboarditem"."deleted")
+ AND ("posthog_annotation"."dashboard_id" IS NULL
+ OR NOT "posthog_dashboard"."deleted")
AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 99999))
@@ -453,8 +459,13 @@
"posthog_user"."email_opt_in"
FROM "posthog_annotation"
LEFT OUTER JOIN "posthog_dashboarditem" ON ("posthog_annotation"."dashboard_item_id" = "posthog_dashboarditem"."id")
+ LEFT OUTER JOIN "posthog_dashboard" ON ("posthog_annotation"."dashboard_id" = "posthog_dashboard"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_annotation"."created_by_id" = "posthog_user"."id")
WHERE (NOT "posthog_annotation"."deleted"
+ AND ("posthog_annotation"."dashboard_item_id" IS NULL
+ OR NOT "posthog_dashboarditem"."deleted")
+ AND ("posthog_annotation"."dashboard_id" IS NULL
+ OR NOT "posthog_dashboard"."deleted")
AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 99999))
@@ -955,7 +966,13 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
+ LEFT OUTER JOIN "posthog_dashboarditem" ON ("posthog_annotation"."dashboard_item_id" = "posthog_dashboarditem"."id")
+ LEFT OUTER JOIN "posthog_dashboard" ON ("posthog_annotation"."dashboard_id" = "posthog_dashboard"."id")
WHERE (NOT "posthog_annotation"."deleted"
+ AND ("posthog_annotation"."dashboard_item_id" IS NULL
+ OR NOT "posthog_dashboarditem"."deleted")
+ AND ("posthog_annotation"."dashboard_id" IS NULL
+ OR NOT "posthog_dashboard"."deleted")
AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 99999))
@@ -1045,8 +1062,13 @@
"posthog_user"."email_opt_in"
FROM "posthog_annotation"
LEFT OUTER JOIN "posthog_dashboarditem" ON ("posthog_annotation"."dashboard_item_id" = "posthog_dashboarditem"."id")
+ LEFT OUTER JOIN "posthog_dashboard" ON ("posthog_annotation"."dashboard_id" = "posthog_dashboard"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_annotation"."created_by_id" = "posthog_user"."id")
WHERE (NOT "posthog_annotation"."deleted"
+ AND ("posthog_annotation"."dashboard_item_id" IS NULL
+ OR NOT "posthog_dashboarditem"."deleted")
+ AND ("posthog_annotation"."dashboard_id" IS NULL
+ OR NOT "posthog_dashboard"."deleted")
AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 99999))
@@ -1208,7 +1230,13 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
+ LEFT OUTER JOIN "posthog_dashboarditem" ON ("posthog_annotation"."dashboard_item_id" = "posthog_dashboarditem"."id")
+ LEFT OUTER JOIN "posthog_dashboard" ON ("posthog_annotation"."dashboard_id" = "posthog_dashboard"."id")
WHERE (NOT "posthog_annotation"."deleted"
+ AND ("posthog_annotation"."dashboard_item_id" IS NULL
+ OR NOT "posthog_dashboarditem"."deleted")
+ AND ("posthog_annotation"."dashboard_id" IS NULL
+ OR NOT "posthog_dashboard"."deleted")
AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 99999))
diff --git a/posthog/api/test/test_annotation.py b/posthog/api/test/test_annotation.py
index 5f49e2f87c45..d2e96cdd1db7 100644
--- a/posthog/api/test/test_annotation.py
+++ b/posthog/api/test/test_annotation.py
@@ -1,5 +1,6 @@
+from collections.abc import Callable
from datetime import datetime, timedelta
-from typing import Optional
+from typing import Any, Optional
from zoneinfo import ZoneInfo
from posthog.test.base import APIBaseTest, FuzzyInt, QueryMatchingTest, snapshot_postgres_queries_context
@@ -477,6 +478,66 @@ class TestAnnotation(APIBaseTest, QueryMatchingTest):
assert response.json()["attr"] == "dashboard_item"
assert response.json()["code"] == "does_not_exist"
+ @parameterized.expand(
+ [
+ (
+ "insight",
+ lambda tc: (
+ insight := Insight.objects.create(team=tc.team, name="My Insight"),
+ Annotation.objects.create(
+ organization=tc.organization,
+ team=tc.team,
+ created_by=tc.user,
+ content="Insight annotation",
+ dashboard_item=insight,
+ ),
+ ),
+ ),
+ (
+ "dashboard",
+ lambda tc: (
+ dashboard := Dashboard.objects.create(team=tc.team, name="My Dashboard"),
+ Annotation.objects.create(
+ organization=tc.organization,
+ team=tc.team,
+ created_by=tc.user,
+ content="Dashboard annotation",
+ scope=Annotation.Scope.DASHBOARD,
+ dashboard=dashboard,
+ ),
+ ),
+ ),
+ ]
+ )
+ def test_annotations_attached_to_soft_deleted_parent_are_hidden(
+ self, _kind: str, create_annotation: Callable[[Any], tuple[Insight | Dashboard, Annotation]]
+ ) -> None:
+ parent, annotation = create_annotation(self)
+
+ list_response = self.client.get(f"/api/projects/{self.team.id}/annotations/")
+ assert annotation.id in {a["id"] for a in list_response.json()["results"]}
+
+ parent.deleted = True
+ parent.save()
+
+ list_response = self.client.get(f"/api/projects/{self.team.id}/annotations/")
+ assert annotation.id not in {a["id"] for a in list_response.json()["results"]}
+
+ retrieve_response = self.client.get(f"/api/projects/{self.team.id}/annotations/{annotation.id}/")
+ assert retrieve_response.status_code == status.HTTP_404_NOT_FOUND
+
+ patch_response = self.client.patch(
+ f"/api/projects/{self.team.id}/annotations/{annotation.id}/",
+ {"content": "edited"},
+ )
+ assert patch_response.status_code == status.HTTP_404_NOT_FOUND
+
+ parent.deleted = False
+ parent.save()
+
+ list_response = self.client.get(f"/api/projects/{self.team.id}/annotations/")
+ assert annotation.id in {a["id"] for a in list_response.json()["results"]}
+
def test_creating_annotation_with_nonexistent_insight_returns_400(self) -> None:
response = self.client.post(
f"/api/projects/{self.team.id}/annotations/",