Answers.Body: <p>I figured this while posting so I thought I'd just add it here anyway for others to reference.</p>
  
  <p>To retrieve a Soft Deleted Element using the <code>getElementById</code> method you can pass the fourth argument to the <code>getElementById</code> method and define $criteria setting <code>trashed</code> to <code>true</code>, which will include Soft Deleted items in your results:</p>
  
  <pre><code>$element = Craft::$app-&gt;elements-&gt;getElementById($elementInfo['elementId'], MyCustomElement::class, $elementInfo['siteId'], [
      'trashed' =&gt; true
  ]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-06T21:17:10.653Z
Answers.LastActivityDate: 2019-08-06T21:17:10.653Z
Answers.UserDisplayName: Ben Parizek
Body: <p>I have a Custom Element where the Soft Delete functionality is not necessary. Right now, existing users of this plugin may have thousands of Soft Deleted Elements in their Elements table that don't need to be there.</p>
  
  <p>I am writing a migration that removes those, and, making some other updates so that when they get deleted in the first place, they get hard deleted.</p>
  
  <p>Where I'm not clear on what the appropriate path forward is how to delete the existing Soft Deleted Elements. As far as I can tell, running the Garbage Cleanup <code>hardDelete</code> method is not really the right way to do this as I can't target a specific Custom Element of my choosing - it's all or nothing. I don't want to delete Soft Deletes that may be important for users, I just want to delete the ones in our plugin.</p>
  
  <p>Next,it seems I can't use <code>deleteElementById</code> as it doesn't give me a way to indicate I want to Hard Delete, so I need to use the <code>deleteElement</code> method called by the <code>deleteElementById</code> method and recreate any code necessary to convert the ID into an actual Element class that I can pass to the <code>deleteElement</code> method. Something like:</p>
  
  <pre><code>foreach ($results as $key =&gt; $elementInfo) {
      $element = Craft::$app-&gt;elements-&gt;getElementById($elementInfo['elementId'], MyCustomElement::class, $elementInfo['siteId']);
  
      if ($element &amp;&amp; !Craft::$app-&gt;elements-&gt;deleteElement($element, true)) {
         // Fail
     }
  
     // Success
  }
  </code></pre>
  
  <p>However, this doesn't appear to work either because <code>getElementById</code> returns <code>null</code>, I expect because it's excluding Soft Deleted items from what it's retrieving.</p>
  
Tags: <plugin-development><elementquery><soft-deletes>
Title: What is the appropriate way to query and Hard Delete existing Soft Deleted Elements for a Custom Element?
ThreadText: # What is the appropriate way to query and Hard Delete existing Soft Deleted Elements for a Custom Element?
  
  (Asked by: username_0 on 2019-08-06)
  
  I have a Custom Element where the Soft Delete functionality is not necessary. Right now, existing users of this plugin may have thousands of Soft Deleted Elements in their Elements table that don't need to be there.
  
  I am writing a migration that removes those, and, making some other updates so that when they get deleted in the first place, they get hard deleted.
  
  Where I'm not clear on what the appropriate path forward is how to delete the existing Soft Deleted Elements. As far as I can tell, running the Garbage Cleanup `hardDelete` method is not really the right way to do this as I can't target a specific Custom Element of my choosing - it's all or nothing. I don't want to delete Soft Deletes that may be important for users, I just want to delete the ones in our plugin.
  
  Next,it seems I can't use `deleteElementById` as it doesn't give me a way to indicate I want to Hard Delete, so I need to use the `deleteElement` method called by the `deleteElementById` method and recreate any code necessary to convert the ID into an actual Element class that I can pass to the `deleteElement` method. Something like:
  
  ```
  foreach ($results as $key => $elementInfo) {
      $element = Craft::$app->elements->getElementById($elementInfo['elementId'], MyCustomElement::class, $elementInfo['siteId']);
  
      if ($element && !Craft::$app->elements->deleteElement($element, true)) {
         // Fail
     }
  
     // Success
  }
  
  ```
  
  However, this doesn't appear to work either because `getElementById` returns `null`, I expect because it's excluding Soft Deleted items from what it's retrieving.
  
  **Tags:** plugin-development, elementquery, soft-deletes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-08-06. Score: 2
  
  I figured this while posting so I thought I'd just add it here anyway for others to reference.
  
  To retrieve a Soft Deleted Element using the `getElementById` method you can pass the fourth argument to the `getElementById` method and define $criteria setting `trashed` to `true`, which will include Soft Deleted items in your results:
  
  ```
  $element = Craft::$app->elements->getElementById($elementInfo['elementId'], MyCustomElement::class, $elementInfo['siteId'], [
      'trashed' => true
  ]);
  
  ```
  
  
  ---
  
  