Deleting users with Convex Auth
What's the best practice for deleting users when using Convex Auth? There aren't any examples in the docs, and removing only the row on the
users
table throws an error if you try to sign up with the same email.
12 Replies
Use a delete function and delete by _id
ctx.db.delete(userId)
Yes, that's what I've done. It doesn't delete other auth rows which reference the userId
Ah, you’ll need to query for those rows then delete the items that are referenced by the userId
I can help a bit more when I get home if you haven’t sorted it.
I wasn't sure if there was a built-in function to Convex Auth that would help with that since it sets up all its own tables for
authAccounts
, authRateLimits
, authSessions
etc. — there are several places to potentially lookconst tags = await ctx.db.query("tags")
.withIndex("by_orgId", (q) => q.eq(("orgId"), args.id as Id<"organization">))
.filter((q) => q.eq(q.field("orgId"), args.id)).collect();
for (const tag of tags) {
await ctx.db.delete(tag._id as Id<"tags">);
}
Here’s an example
In this example, if I’m deleting an organization, I want any related tags to be deleted.
Thanks! This is getting closer. If I wanted to relocate a function like this (e.g.
deleteAllTagsForUser
within tags.ts
) and then reference that internally from another mutation, would that work?You would need to use an action if you’re wanting to call an internal mutation
Thanks! I think I'll keep it all in the mutation for now
Yeah I think you should be okay using mutations
Always trying to prematurely optimize 😭
Thanks for your help!
Lol I hear ya. Let me know if I can be of more help.
if it helps anyone else, i wrote this function which works great.
if anyone is curious, this is my userMutation: