{"record":{"id":"834a6f4f37926696","repo":"Budibase/budibase","slug":"unable-to-delete-self","errorCode":null,"errorMessage":"Unable to delete self.","messagePattern":"Unable to delete self\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/worker/src/api/controllers/global/users.ts","lineNumber":184,"sourceCode":"    // Need to get the _rev of the user doc to update\n    const userById = await platform.users.getUserDoc(userByEmail.userId)\n    await platform.users.updateUserDoc({\n      ...userById,\n      email,\n      ssoId,\n    })\n    ctx.body = { message: \"SSO support added.\" }\n  } catch (err: any) {\n    ctx.throw(err.status || 400, err?.message || err)\n  }\n}\n\nconst bulkDelete = async (\n  users: Array<UserIdentifier>,\n  currentUserId: string\n) => {\n  if (users.find(u => u.userId === currentUserId)) {\n    throw new Error(\"Unable to delete self.\")\n  }\n  return await userSdk.db.bulkDelete(users)\n}\n\nconst bulkCreate = async (users: User[], groupIds: string[]) => {\n  if (!env.SELF_HOSTED && users.length > MAX_USERS_UPLOAD_LIMIT) {\n    throw new Error(\n      \"Max limit for upload is 1000 users. Please reduce file size and try again.\"\n    )\n  }\n  return await userSdk.db.bulkCreate(users, groupIds)\n}\n\nexport const bulkUpdate = async (\n  ctx: Ctx<BulkUserRequest, BulkUserResponse>\n) => {\n  const currentUserId = ctx.user._id\n  const input = ctx.request.body","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/worker/src/api/controllers/global/users.ts#L166-L202","documentation":"bulkDelete iterates the requested user identifiers and rejects the whole operation if any target userId equals the currently authenticated user's id. This prevents a user from deleting their own account (which would break their session and tenant state). Called both directly by bulkDelete and via bulkUpdate flows that can delete.","triggerScenarios":"Calling the bulk user delete endpoint (or a bulk update that routes into bulkDelete) with a payload that includes the current user's own userId among the users to delete.","commonSituations":"An admin selects 'all users' in a UI checkbox list including themselves; scripted cleanup that deletes every user in a tenant; stale client-side state where the admin's own row wasn't filtered out.","solutions":["Remove the current user's id from the delete list before calling the API","In the UI, disable/deselect the row representing the logged-in admin","Delete other users first and have another admin remove the last one if needed","Handle the error response and inform the user they cannot delete their own account"],"exampleFix":"// before\nawait api.bulkDelete(allUsers.map(u => ({ userId: u._id })))\n// after\nconst deletable = allUsers.filter(u => u._id !== currentUser._id)\nawait api.bulkDelete(deletable.map(u => ({ userId: u._id })))","handlingStrategy":"validation","validationCode":"if (users.some(u => u.userId === currentUserId)) {\n  throw new Error(\"Cannot include the current user in a bulk delete\")\n}","typeGuard":"null","tryCatchPattern":"try {\n  await api.bulkDelete(userIds)\n} catch (e) {\n  if (e.message.includes(\"Unable to delete self\")) {\n    notify(\"You cannot delete your own account\")\n  }\n}","preventionTips":["Filter out the logged-in user's id before submitting bulk deletes","In admin UIs, prevent selecting your own account row","Review bulk payloads in scripts for inclusion of the caller's id"],"tags":["users","validation","api","self-delete"],"backgroundTag":"self-deletion-prevented","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}