From 438168f2ab742c347a6d3a73ee06acd885dad011 Mon Sep 17 00:00:00 2001 From: Manoj Date: Mon, 20 Oct 2025 19:20:16 +0530 Subject: [PATCH] feat: clean up Neo4j graph nodes when deleting user account --- apps/webapp/app/models/user.server.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/apps/webapp/app/models/user.server.ts b/apps/webapp/app/models/user.server.ts index 39d568a..834c20e 100644 --- a/apps/webapp/app/models/user.server.ts +++ b/apps/webapp/app/models/user.server.ts @@ -2,6 +2,7 @@ import type { Prisma, User } from "@core/database"; import type { GoogleProfile } from "@coji/remix-auth-google"; import { prisma } from "~/db.server"; import { env } from "~/env.server"; +import { runQuery } from "~/lib/neo4j.server"; export type { User } from "@core/database"; type FindOrCreateMagicLink = { @@ -252,6 +253,23 @@ export async function deleteUser(id: User["id"]) { throw new Error("User not found"); } + // Delete all user-related nodes from the Neo4j knowledge graph + try { + // Delete all nodes (Episodes, Entities, Statements, Spaces, Documents, Clusters) + // and their relationships where userId matches + await runQuery( + ` + MATCH (n {userId: $userId}) + DETACH DELETE n + `, + { userId: id } + ); + console.log(`Deleted all graph nodes for user ${id}`); + } catch (error) { + console.error("Failed to delete graph nodes:", error); + // Continue with deletion even if graph cleanup fails + } + // If workspace exists, delete all workspace-related data // Most models DON'T have onDelete: Cascade, so we must delete manually if (user.Workspace) {