#Cascade deletion
3 messages · Page 1 of 1 (latest)
Hi @hollow leaf. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, there are several ways to implement cascade deletion in Convex. Here are the main options:
- Using Database Triggers: Convex now supports database triggers, which can be used to implement cascade deletes. Here's an example:
triggers.register("users", async (ctx, change) => {
if (change.operation === "delete") {
for await (const message of ctx.db.query("messages")
.withIndex("owner", q=>q.eq("owner", change.id))) {
await ctx.db.delete(message._id);
}
}
});
This trigger will delete all messages owned by a user when that user is deleted. Source (https://stack.convex.dev/triggers#cascade-deletes)
- Manual Implementation: You can manually implement cascade deletion in your mutation function. Here's a basic example:
deleteProject:
ctx.db.delete(project._id)
tasks = ctx.db.query("tasks").withIndex(...)
for (task of tasks) {
ctx.db.delete(task._id)
}
// Repeat for chats, messages, etc.
Source (https://discord.com/channels/1019350475847499849/1240426544225063064)
- Using Convex Ents: Convex Ents, a higher-level data modeling library for Convex, provides more advanced options for cascade deletion. However, it's worth noting that Ents is currently in maintenance mode. Source (https://stack.convex.dev/ents)