authClient.admin.removeUser doesn't trigger beforeDelete

I'm using the admin plugin. It has authClient.admin.removeUser functionality. the problem is that I set up user beforeDelete to delete all of the images from my s3 bucket before removing the user and it doesn't work. It works if and only if the user deletes himself manually. user deletion from the admin side:
async function handleDelete() {
await authClient.admin.removeUser({
//@ts-expect-error
userId,
});
router.push("/admin/dashboard");
router.refresh();
setOpen(false);
}
async function handleDelete() {
await authClient.admin.removeUser({
//@ts-expect-error
userId,
});
router.push("/admin/dashboard");
router.refresh();
setOpen(false);
}
better-auth instance:
const auth = betterAuth({
user: {
deleteUser: {
beforeDelete: async (user) => {
// images deletion logic
const userChatRoomImages = (
await db.chatRoom.findUnique({
where: {
userId: Number(user.id),
},
select: { images: true },
})
)?.images;
userChatRoomImages?.map(async (image) => await deleteImage(image.key));
},
enabled: true,
},
}
})
const auth = betterAuth({
user: {
deleteUser: {
beforeDelete: async (user) => {
// images deletion logic
const userChatRoomImages = (
await db.chatRoom.findUnique({
where: {
userId: Number(user.id),
},
select: { images: true },
})
)?.images;
userChatRoomImages?.map(async (image) => await deleteImage(image.key));
},
enabled: true,
},
}
})
3 Replies
Ping
Ping4w ago
We have a method to allow the user to delete their own account. (assuming you set user.deleteUser.enabled to true) user.deleteUser is that configuration. It's not related to the general before or after events of a user being delete.
Maqed
MaqedOP4w ago
Alright, what's "the general before or after events of a user being delete" Could you please tell me where is it in the docs? I set user.deleteUser.enabled to true
Ping
Ping4w ago
You have to use hooks and run checks to find that case if you want events for it. Otherwise, which is more what we recommend for now, is to just write code to call a function to clean up the user for every user delete case

Did you find this page helpful?