: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(alerts): delete alerts when parent insight is soft-deleted (#58236)
Luca Zanic5fde181bb2b
diff --git a/posthog/api/insight.py b/posthog/api/insight.pyindex 233800b40887..47156edbf037 100644--- a/posthog/api/insight.py+++ b/posthog/api/insight.py@@ -691,6 +691,8 @@ class InsightSerializer(InsightBasicSerializer):if validated_data.get("deleted", False):DashboardTile.objects_including_soft_deleted.filter(insight__id=instance.id).update(deleted=True)+ for alert in instance.alertconfiguration_set.all():+ alert.delete()else:dashboards = validated_data.pop("dashboards", None)if dashboards is not None:@@ -698,7 +700,8 @@ class InsightSerializer(InsightBasicSerializer):updated_insight = super().update(instance, validated_data)if not updated_insight.are_alerts_supported:- instance.alertconfiguration_set.all().delete()+ for alert in instance.alertconfiguration_set.all():+ alert.delete()self._log_insight_update(before_update, dashboards_before_change, updated_insight)diff --git a/posthog/api/test/test_alert.py b/posthog/api/test/test_alert.pyindex 4021433455d4..6a5d32f147b1 100644--- a/posthog/api/test/test_alert.py+++ b/posthog/api/test/test_alert.py@@ -361,6 +361,46 @@ class TestAlert(APIBaseTest, QueryMatchingTest):response = self.client.get(f"/api/projects/{self.team.id}/alerts/{alert['id']}")assert response.status_code == status.HTTP_404_NOT_FOUND+ def test_alert_is_deleted_on_insight_soft_delete(self) -> None:+ another_insight = self.client.post(+ f"/api/projects/{self.team.id}/insights", data=self.default_insight_data+ ).json()+ creation_request = {+ "insight": another_insight["id"],+ "subscribed_users": [self.user.id],+ "condition": {"type": AlertConditionType.ABSOLUTE_VALUE},+ "config": {"type": "TrendsAlertConfig", "series_index": 0},+ "threshold": {"configuration": {"type": InsightThresholdType.ABSOLUTE, "bounds": {}}},+ "name": "alert name",+ }+ alert = self.client.post(f"/api/projects/{self.team.id}/alerts", creation_request).json()+ alert_id = alert["id"]++ linked_hog_function = HogFunction.objects.create(+ team=self.team,+ name="Slack notification for alert",+ type="internal_destination",+ hog="return 1",+ enabled=True,+ filters={+ "events": [{"id": "$insight_alert_firing", "type": "events"}],+ "properties": [{"key": "alert_id", "value": alert_id, "operator": "exact", "type": "event"}],+ },+ )++ patch_response = self.client.patch(+ f"/api/projects/{self.team.id}/insights/{another_insight['id']}",+ data={"deleted": True},+ )+ assert patch_response.status_code == status.HTTP_200_OK++ response = self.client.get(f"/api/projects/{self.team.id}/alerts/{alert_id}")+ assert response.status_code == status.HTTP_404_NOT_FOUND++ linked_hog_function.refresh_from_db()+ assert linked_hog_function.deleted is True+ assert linked_hog_function.enabled is False+def test_delete_alert_cleans_up_hog_functions(self) -> None:creation_request = {"insight": self.insight["id"],