DaReal
DaReal8mo ago

Deleting Document and Its Referenced Documents

Hello Support, I would like to know if it is possible and how i can delete a document and also all documents where it has a relationship with the document i am deleting. So i have a "Users" and "Transactions" table (there are more but this is for simplicity sake). I want to delete a specific document/user from the "Users" table and I also want to be able to delete all documents in the "Transactions" table that has a relation ship with the specific user i am deleting. I was thinking of searching for all documents that has the id referenced in the "Transactions" table using the Index but i was wondering if there is a better approach. I hope you understand. Thanks in advance.
16 Replies
erquhart
erquhart8mo ago
Nope you're right, get all of the documents that match and delete in a loop. You can async map over the list to do it in parallel, too, it's quite fast. @kapa.ai can you give an example?
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Sure, I can provide examples based on the knowledge sources provided. However, it would be helpful if you could specify what kind of example you're looking for. Are you interested in an example of how to use Convex in a React application, how to upload files using Convex, or perhaps how to debug functions in Convex? Please provide more context so I can give you the most relevant example. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
erquhart
erquhart8mo ago
let's see if the ai assistant can do this without further prompting
kapa.ai
kapa.ai8mo ago
<@385147894409199627> kapa.ai is still learning and improving, please let us know how it did by reacting below
erquhart
erquhart8mo ago
@kapa.ai I would like to know if it is possible and how i can delete a document and also all documents where it has a relationship with the document i am deleting. So i have a "Users" and "Transactions" table (there are more but this is for simplicity sake). I want to delete a specific document/user from the "Users" table and I also want to be able to delete all documents in the "Transactions" table that has a relation ship with the specific user i am deleting.
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, it is possible to delete a document and all related documents in Convex. This can be achieved using the cascading delete feature of Convex Ents, a higher-level data modeling and querying library built on top of Convex. Here's a general overview of how you can do it: 1. Cascading Deletions: Convex Ents supports cascading deletions. The default behavior deletes all related documents that require the existence of a given document in a single transaction. This preserves the “no dangling references” invariant, as long as you don’t expect to have many related documents. This can be useful for your case where you want to delete a user and all their related transactions. source (https://stack.convex.dev/ents#cascading-deletes-soft-deletion-and-scheduled-deletion) 2. Deleting Documents: Given an existing document ID, the document can be removed from the table with the db.delete method. Here's an example of how to delete a document:
import { mutation } from "./_generated/server";
import { v } from "convex/values";

export const deleteTask = mutation({
args: { id: v.id("tasks") },
handler: async (ctx, args) => {
await ctx.db.delete(args.id);
},
});
import { mutation } from "./_generated/server";
import { v } from "convex/values";

export const deleteTask = mutation({
args: { id: v.id("tasks") },
handler: async (ctx, args) => {
await ctx.db.delete(args.id);
},
});
source (https://docs.convex.dev/database/writing-data#deleting-documents) 3. Deleting All Documents in a Table: You can delete all documents in a table by clicking on the overflow menu at the top of the data page and clicking "Clear Table". This action will delete all documents in the table, without deleting the table itself. source (https://docs.convex.dev/dashboard/deployments/data#clear-a-table) Please note that you need to be careful when deleting documents, especially when using cascading deletes, as it can lead to the deletion of a large number of documents in a single transaction. This can potentially grind the whole database to a halt, something Convex does not allow, instead failing the mutation. source (https://stack.convex.dev/ents#cascading-deletes-soft-deletion-and-scheduled-deletion) Also, if you're not using Convex Ents and want to manually delete related documents, you would need to query for the documents that need deleting, then loop over them with db.delete. This way you can be very explicit about how the deletion "cascades" through your database. source (https://discord.com/channels/1019350475847499849/1210696251977236550) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@385147894409199627> kapa.ai is still learning and improving, please let us know how it did by reacting below
erquhart
erquhart8mo ago
Not the example I was hoping for I'm sure this is covered directly in the docs but I'm not finding a great example there. But you are 100% on the right track. There's a Stack article that would be helpful, I'll grab the link
DaReal
DaRealOP8mo ago
Alright. Thanks! I don't know about the Convex Ents, but i will do a quick search and see.
erquhart
erquhart8mo ago
This is helpful for understanding best practices around relationships in Convex: https://stack.convex.dev/relationship-structures-let-s-talk-about-schemas
Relationship Structures: Let's Talk About Schemas
In this post we’ll look at some patterns for structuring relationships in the Convex database.
erquhart
erquhart8mo ago
What I'd specifically add to what you're currently doing, besides recommending async mapping, is to make sure to have an index for the query that collects the related rows.
DaReal
DaRealOP8mo ago
Yes i do have an index for the user on all tables that references the user I didnt find any reference to the .then() method in the docs.
erquhart
erquhart8mo ago
Any promise will have a then() method, but you'll typically use async/await instead
DaReal
DaRealOP8mo ago
Alright thanks. 😁
Michal Srb
Michal Srb8mo ago
Your approach is correct. Pseudo code:
deleteUser:
ctx.db.delete user._id
transactions = ctx.db.query("transactions").withIndex(....
for (transaction of transactions) {
ctx.db.delete transaction._id
}
deleteUser:
ctx.db.delete user._id
transactions = ctx.db.query("transactions").withIndex(....
for (transaction of transactions) {
ctx.db.delete transaction._id
}
Ents provide some more tooling around cascading deletes, like deciding whether to soft delete only or schedule the deletion async (in case there could be a lot of documents so they wouldn't fit in the mutation write limits): https://labs.convex.dev/convex-ents/schema/deletes
Cascading Deletes - Convex Ents
Relations, default values, unique fields and more for Convex
lee
lee3mo ago
for anyone finding this later, there's also this https://stack.convex.dev/triggers#cascade-deletes
Database Triggers
Triggers automatically run code whenever data in a table changes. A library in the convex-helpers npm package allows you to attach trigger functions t...

Did you find this page helpful?