feat: clean up Neo4j graph nodes when deleting user account

This commit is contained in:
Manoj 2025-10-20 19:20:16 +05:30
parent 606dfa2462
commit 438168f2ab

View File

@ -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) {