{"record":{"id":"55d2baa959836507","repo":"phacility/phabricator","slug":"can-t-remove-primary-email","errorCode":null,"errorMessage":"Can't remove primary email!","messagePattern":"Can't remove primary email!","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/applications/people/editor/PhabricatorUserEditor.php","lineNumber":238,"sourceCode":"    PhabricatorUserEmail $email) {\n\n    $actor = $this->requireActor();\n\n    if (!$user->getID()) {\n      throw new Exception(pht('User has not been created yet!'));\n    }\n    if (!$email->getID()) {\n      throw new Exception(pht('Email has not been created yet!'));\n    }\n\n    $user->openTransaction();\n      $user->beginWriteLocking();\n\n        $user->reload();\n        $email->reload();\n\n        if ($email->getIsPrimary()) {\n          throw new Exception(pht(\"Can't remove primary email!\"));\n        }\n        if ($email->getUserPHID() != $user->getPHID()) {\n          throw new Exception(pht('Email not owned by user!'));\n        }\n\n        $destruction_engine = id(new PhabricatorDestructionEngine())\n          ->setWaitToFinalizeDestruction(true)\n          ->destroyObject($email);\n\n        $log = PhabricatorUserLog::initializeNewLog(\n          $actor,\n          $user->getPHID(),\n          PhabricatorRemoveEmailUserLogType::LOGTYPE);\n        $log->setOldValue($email->getAddress());\n        $log->save();\n\n      $user->endWriteLocking();\n    $user->saveTransaction();","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/applications/people/editor/PhabricatorUserEditor.php#L220-L256","documentation":"Guard inside the write-locked transaction of PhabricatorUserEditor::removeEmail: the email being detached is flagged as the user's primary address, and a user must always keep a primary email, so removal is refused.","triggerScenarios":"Thrown at src/applications/people/editor/PhabricatorUserEditor.php:238 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["First switch the primary to another verified address with changePrimaryEmail, then remove this one.","If the account should lose its only address, disable or delete the account instead of removing the primary email."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","contentChangedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}