phacility/phabricator · error · Exception

Can't remove primary email!

Error message

Can't remove primary email!

What it means

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.

Solutions

  1. First switch the primary to another verified address with changePrimaryEmail, then remove this one.
  2. If the account should lose its only address, disable or delete the account instead of removing the primary email.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/applications/people/editor/PhabricatorUserEditor.php:238 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phacility/phabricator@5720a38cfe (2026-08-21). Data as JSON: /api/errors/55d2baa959836507. Report an issue: GitHub.

Appendix: source

Thrown at src/applications/people/editor/PhabricatorUserEditor.php:238

    PhabricatorUserEmail $email) {

    $actor = $this->requireActor();

    if (!$user->getID()) {
      throw new Exception(pht('User has not been created yet!'));
    }
    if (!$email->getID()) {
      throw new Exception(pht('Email has not been created yet!'));
    }

    $user->openTransaction();
      $user->beginWriteLocking();

        $user->reload();
        $email->reload();

        if ($email->getIsPrimary()) {
          throw new Exception(pht("Can't remove primary email!"));
        }
        if ($email->getUserPHID() != $user->getPHID()) {
          throw new Exception(pht('Email not owned by user!'));
        }

        $destruction_engine = id(new PhabricatorDestructionEngine())
          ->setWaitToFinalizeDestruction(true)
          ->destroyObject($email);

        $log = PhabricatorUserLog::initializeNewLog(
          $actor,
          $user->getPHID(),
          PhabricatorRemoveEmailUserLogType::LOGTYPE);
        $log->setOldValue($email->getAddress());
        $log->save();

      $user->endWriteLocking();
    $user->saveTransaction();

View on GitHub (pinned to 5720a38cfe)