Answers.Body: <p>Civi will set another address as Primary if you use its tools to delete but it can't do that if you rip data out from under its feet.</p>
  <p>Deleting things directly from the database is usually a bad idea! If you delete manually you also need to manually assign another address as primary.</p>
  <p>However, if you use the API to delete the email address another one will be assigned as the primary automatically.</p>
  <p>If you want to delete all your on_hold addresses (not saying that is a good idea ... but that's what you describe), then</p>
  <pre><code>$results = \Civi\Api4\Email::delete()
    -&gt;addWhere('on_hold', 'IN', [1, 2])
    -&gt;execute();
  </code></pre>
  <p>will remove them all and assign new primaries.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-16T16:14:37.187Z
Answers.LastActivityDate: 2023-01-16T16:14:37.187Z
Answers.UserDisplayName: Aidan
Body: <p>We are doing a bit of DB cleanup and one area we are evaluating is on-hold emails. I have run some tests on our staging site and I think we have a good path forward to remove them. However, if the email that we removed is the Primary Email, Civi does not set another email address as Primary. Is there a quick way to set a Primary email if one is not present?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-13T15:42:57.383Z
Comments.Text: How are you deleting them?  The UI doesn't let you delete a primary address.
Comments.UserDisplayName: Aidan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-13T19:03:22.763Z
Comments.Text: @Aidan, We are deleting them straight from the database.
Comments.UserDisplayName: zach
Tags: <civimail><email><apiv4>
Title: Delete On_Hold emails and reassign Primary Email
ThreadText: # Delete On_Hold emails and reassign Primary Email
  
  (Asked by: username_0 on 2023-01-13)
  
  We are doing a bit of DB cleanup and one area we are evaluating is on-hold emails. I have run some tests on our staging site and I think we have a good path forward to remove them. However, if the email that we removed is the Primary Email, Civi does not set another email address as Primary. Is there a quick way to set a Primary email if one is not present?
  
  **Tags:** civimail, email, apiv4
  
  > **Comments:**
  > **username_1**: How are you deleting them?  The UI doesn't let you delete a primary address.  
  > **username_0**: @username_1, We are deleting them straight from the database.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-01-16. Score: 3 | Accepted answer
  
  Civi will set another address as Primary if you use its tools to delete but it can't do that if you rip data out from under its feet.
  
  Deleting things directly from the database is usually a bad idea! If you delete manually you also need to manually assign another address as primary.
  
  However, if you use the API to delete the email address another one will be assigned as the primary automatically.
  
  If you want to delete all your on\_hold addresses (not saying that is a good idea ... but that's what you describe), then
  
  ```
  $results = \Civi\Api4\Email::delete()
    ->addWhere('on_hold', 'IN', [1, 2])
    ->execute();
  
  ```
  
  will remove them all and assign new primaries.
  
  
  ---
  
  