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(file-system): allow deleting ref-less desktop canvas rows (#67932)

Georges-Antoine Assid3f518e5f090
diff --git a/posthog/api/file_system/file_system.py b/posthog/api/file_system/file_system.py
index d80a17bd6aeb..53cede2ea742 100644
--- a/posthog/api/file_system/file_system.py
+++ b/posthog/api/file_system/file_system.py
@@ -517,6 +517,16 @@ class FileSystemViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
}
)
+ def _allow_delete_without_ref(self, entry: FileSystem) -> bool:
+ """Whether a registered-type row with no ref may be deleted as a bare row.
+
+ On the web surface every registered row references a real object, so a
+ ref-less row is a data-integrity error we refuse to delete. Surfaces where
+ registered types can legitimately be ref-less (desktop canvases store their
+ source in `meta`, not a backing Dashboard) override this to allow it.
+ """
+ return False
+
def _ensure_can_delete(self, entry: FileSystem) -> None:
stack: list[FileSystem] = [entry]
seen: set[str] = set()
@@ -557,7 +567,7 @@ class FileSystemViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
if not is_file_system_type_registered(current.type):
continue
- if remaining == 0 and not current.ref:
+ if remaining == 0 and not current.ref and not self._allow_delete_without_ref(current):
raise serializers.ValidationError(
{"detail": f"Cannot delete type '{current.type}' without a reference."}
)
@@ -595,6 +605,9 @@ class FileSystemViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
return deleted_objects
if not entry.ref:
+ if self._allow_delete_without_ref(entry):
+ entry.delete()
+ return deleted_objects
raise serializers.ValidationError({"detail": f"Cannot delete type '{entry.type}' without a reference."})
entry_path = entry.path
@@ -1027,6 +1040,14 @@ class DesktopFileSystemViewSet(FileSystemViewSet):
file_system_surface = "desktop"
+ def _allow_delete_without_ref(self, entry: FileSystem) -> bool:
+ # Desktop canvases are `dashboard`-typed rows whose source lives in `meta`,
+ # not a backing Dashboard, so they legitimately have no ref. Delete the bare
+ # row (nothing to cascade to) rather than refusing. Scope this to `dashboard`
+ # only — any other registered type with no ref is still a data-integrity
+ # error we refuse to delete, even on the desktop surface.
+ return entry.type == "dashboard"
+
def perform_create(self, serializer: serializers.BaseSerializer) -> None:
super().perform_create(serializer)
instance = cast(FileSystem, serializer.instance)
diff --git a/posthog/api/file_system/test/test_canvas_publish.py b/posthog/api/file_system/test/test_canvas_publish.py
index e04a59c7d164..791c929bdce0 100644
--- a/posthog/api/file_system/test/test_canvas_publish.py
+++ b/posthog/api/file_system/test/test_canvas_publish.py
@@ -95,3 +95,42 @@ class TestDesktopCanvasPublishAPI(APIBaseTest):
bad = self.client.patch(self._canvas_url(item_id), {"prompt": "only a prompt"})
self.assertEqual(bad.status_code, status.HTTP_400_BAD_REQUEST, bad.json())
self.assertIn("code", bad.json())
+
+ def test_delete_canvas_removes_ref_less_dashboard_row(self):
+ # Desktop canvases are `dashboard`-typed rows with no ref; deleting one must not
+ # trip the "without a reference" guard meant for real object-backed rows.
+ item_id = self._create_dashboard()
+
+ response = self.client.delete(f"/api/projects/{self.team.id}/desktop_file_system/{item_id}/")
+
+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.content)
+ self.assertFalse(FileSystem.objects.filter(id=item_id).exists())
+
+ def test_delete_channel_folder_cascades_to_ref_less_canvas(self):
+ # Deleting a channel folder cascades into its ref-less canvas children, which must
+ # bare-delete rather than raise in `_ensure_can_delete`.
+ item_id = self._create_dashboard(path="MyChannel/MyCanvas")
+ folder = FileSystem.objects.get(team=self.team, path="MyChannel", type="folder")
+
+ response = self.client.delete(f"/api/projects/{self.team.id}/desktop_file_system/{folder.id}/")
+
+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, response.content)
+ self.assertFalse(FileSystem.objects.filter(id=folder.id).exists())
+ self.assertFalse(FileSystem.objects.filter(id=item_id).exists())
+
+ def test_delete_ref_less_non_dashboard_registered_row_still_refused_on_desktop(self):
+ # The desktop ref-less exemption is scoped to `dashboard` canvases. Any other
+ # registered type with no ref is still a data-integrity error we refuse to delete.
+ file_obj = FileSystem.objects.create(
+ team=self.team,
+ path="MyChannel/OrphanInsight",
+ type="insight",
+ surface="desktop",
+ created_by=self.user,
+ )
+
+ response = self.client.delete(f"/api/projects/{self.team.id}/desktop_file_system/{file_obj.id}/")
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content)
+ self.assertEqual(response.json()["detail"], "Cannot delete type 'insight' without a reference.")
+ self.assertTrue(FileSystem.objects.filter(id=file_obj.id).exists())
diff --git a/posthog/api/file_system/test/test_file_system.py b/posthog/api/file_system/test/test_file_system.py
index 793f116c3d13..c4ff098793df 100644
--- a/posthog/api/file_system/test/test_file_system.py
+++ b/posthog/api/file_system/test/test_file_system.py
@@ -180,6 +180,24 @@ class TestFileSystemAPI(APIBaseTest):
self.assertFalse(FileSystem.objects.filter(pk=file1_obj.pk).exists())
self.assertFalse(FileSystem.objects.filter(pk=file2_obj.pk).exists())
+ def test_delete_ref_less_registered_row_refused_on_web_surface(self):
+ """
+ On the default (web) surface every registered-type row points at a real object via `ref`.
+ A ref-less registered row is a data-integrity error, so deletion is refused, not silently applied.
+ """
+ file_obj = FileSystem.objects.create(
+ team=self.team,
+ path="DeleteMe/RefLessDashboard",
+ type="dashboard",
+ created_by=self.user,
+ )
+
+ delete_response = self.client.delete(f"/api/projects/{self.team.id}/file_system/{file_obj.pk}/")
+
+ self.assertEqual(delete_response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(delete_response.json()["detail"], "Cannot delete type 'dashboard' without a reference.")
+ self.assertTrue(FileSystem.objects.filter(pk=file_obj.pk).exists())
+
def test_unfiled_endpoint_no_content(self):
"""
If there are no relevant items to create (e.g. no FeatureFlags, Experiments, etc.),