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.

feat(flags): reject usage dashboard generation for deleted flags (#82119)

Phil Haacke94c98905937
diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.py
index e72351edf78a..93c722e7fb71 100644
--- a/products/feature_flags/backend/api/feature_flag.py
+++ b/products/feature_flags/backend/api/feature_flag.py
@@ -3097,6 +3097,20 @@ class FeatureFlagViewSet(
return response
+ @staticmethod
+ def _deleted_flag_rejection(feature_flag: FeatureFlag, restore_hint: str) -> Response | None:
+ """Dashboard-generating actions refuse soft-deleted flags: they would recreate the
+ auto-generated insights that the delete_feature_flag_usage_insights sweep deletes."""
+ if not feature_flag.deleted:
+ return None
+ return Response(
+ {
+ "success": False,
+ "error": f"This feature flag has been deleted. Restore it before {restore_hint}.",
+ },
+ status=400,
+ )
+
# No UI surface calls this, since the Usage tab renders its charts inline. It exists for API
# users who want a saved usage dashboard.
@extend_schema(request=None)
@@ -3105,6 +3119,9 @@ class FeatureFlagViewSet(
from products.dashboards.backend.models.dashboard import Dashboard
feature_flag: FeatureFlag = self.get_object()
+ rejection = self._deleted_flag_rejection(feature_flag, "generating a usage dashboard")
+ if rejection is not None:
+ return rejection
try:
# The FK on the flag isn't cleared by a dashboard soft-delete, so look the id up
# through the manager that excludes deleted rows rather than via the FK accessor,
@@ -3138,6 +3155,9 @@ class FeatureFlagViewSet(
@action(methods=["POST"], detail=True)
def enrich_usage_dashboard(self, request: request.Request, **kwargs):
feature_flag: FeatureFlag = self.get_object()
+ rejection = self._deleted_flag_rejection(feature_flag, "enriching its usage dashboard")
+ if rejection is not None:
+ return rejection
usage_dashboard = feature_flag.usage_dashboard
if not usage_dashboard:
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.py
index 53aee37cfaf4..4fdcb0e9664f 100644
--- a/products/feature_flags/backend/api/test/test_feature_flag.py
+++ b/products/feature_flags/backend/api/test/test_feature_flag.py
@@ -5084,6 +5084,17 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
self.assertNotEqual(new_dashboard_id, deleted_dashboard_id)
self.assertTrue(Dashboard.objects.filter(id=new_dashboard_id, deleted=False).exists())
+ @parameterized.expand(["dashboard", "enrich_usage_dashboard"])
+ def test_dashboard_generating_endpoints_reject_a_deleted_flag(self, endpoint: str) -> None:
+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="deleted-flag", deleted=True)
+
+ response = self.client.post(f"/api/projects/{self.team.id}/feature_flags/{flag.id}/{endpoint}")
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn("has been deleted", response.json()["error"])
+ flag.refresh_from_db()
+ self.assertIsNone(flag.usage_dashboard_id)
+
@patch("products.feature_flags.backend.flag_analytics.CACHE_BUCKET_SIZE", 10)
def test_local_evaluation_billing_analytics_for_regular_feature_flag_list(self):
FeatureFlag.objects.all().delete()