DylanDevD
Convex Community13mo ago
11 replies
DylanDev

Deleting Users from Backend Function that are Synced with Clerk

I have a users table that is updated by Clerk sending webhooks with the users over to convex. Now I want to implement a feature where admins on my site can delete users. However, I am running into an error when I run npx convex dev as seen in the screenshot.

This is my backend function that is triggering the error, more specifically, when I imported clerkClient.
import { clerkClient } from "@clerk/nextjs/server";

export const deleteUserAdministrativeAction = mutation({
  args: {
    userToDeleteUsername: v.string(),
  },
  async handler(ctx, args) {
    const userToDelete = await getUserByUsername(ctx, { username: args.userToDeleteUsername });
    const callUser = await getCurrentUser(ctx);
    
    if(userToDelete && callUser) {
      if(await hasRole(ctx, { clerkId: callUser.clerkId, role: "developer" })) {
        try {
          await clerkClient.users.deleteUser(userToDelete.clerkId);
        } catch (error) {
          console.log(error);
        }
      }
    }
  },
});

The error is not a runtime error but rather a compilation error when
npx convex dev
is ran. I can't just delete the user entry in the users table cause that won't wipe it from clerk. How do I fix this issue?
Screenshot_2024-12-20_at_11.17.29_AM.png
Solution
@Tom thank you tom! I ended up splitting the code into a mutation and an action. That way I could use the third party call inside of the backend!
Was this page helpful?