Answers.Body: <p>I'm not sure how well it'd go with 50,000 Assets, but you should just be able to call <code>Craft::$app-&gt;elements-&gt;deleteElement($asset)</code> for each one, or <code>Craft::$app-&gt;elements-&gt;deleteElementById($asset-&gt;id)</code>, as seen in the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelement" rel="nofollow noreferrer">Elements Class Reference</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-28T14:25:29.993Z
Answers.Text: Will that delete the actual file off the server as well? Seems like it would just delete the element in the database
Answers.UserDisplayName: Keith Mancuso
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-28T17:16:49.573Z
Answers.Text: @KeithMancuso it'll eventually trigger the asset element's `afterDelete` method which will take care of deleting the file: https://github.com/craftcms/cms/blob/master/src/elements/Asset.php#L1292-L1300
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-28T05:18:11.890Z
Answers.LastActivityDate: 2018-12-28T05:18:11.890Z
Answers.UserDisplayName: Matt Stein
Body: <p>I'm trying to clean-up the assets in a specific folder that aren't being used at all, i.e. aren't related to anything.</p>
  
  <p>I got the list O.K. just using this</p>
  
  <pre><code>$assets = Asset::find()-&gt;volume('artists')-&gt;kind('image')-&gt;limit(50000)-&gt;ids();
  
  $assetsToDelete = [];
  
  foreach($assets as $asset) {
      $results = (new Query())
          -&gt;select(['id'])
          -&gt;from(['{{%relations}}'])
          -&gt;where(['targetId' =&gt; $asset])
          -&gt;orWhere(['sourceId' =&gt; $asset])
          -&gt;all();
  
      if (!count($results)) {
          $assetsToDelete[]  = $asset;
      }     
  }
  
  return  $this-&gt;asJson($assetsToDelete);
  </code></pre>
  
  <p>Right now I'm just returning all the ids in an array but I'm hoping to find a command to delete the element AND delete the stored asset on the server</p>
  
  <p>Any ideas?</p>
  
Tags: <assets><modules>
Title: How do I delete an asset with module or plugin
ThreadText: # How do I delete an asset with module or plugin
  
  (Asked by: username_0 on 2018-12-27)
  
  I'm trying to clean-up the assets in a specific folder that aren't being used at all, i.e. aren't related to anything.
  
  I got the list O.K. just using this
  
  ```
  $assets = Asset::find()->volume('artists')->kind('image')->limit(50000)->ids();
  
  $assetsToDelete = [];
  
  foreach($assets as $asset) {
      $results = (new Query())
          ->select(['id'])
          ->from(['{{%relations}}'])
          ->where(['targetId' => $asset])
          ->orWhere(['sourceId' => $asset])
          ->all();
  
      if (!count($results)) {
          $assetsToDelete[]  = $asset;
      }     
  }
  
  return  $this->asJson($assetsToDelete);
  
  ```
  
  Right now I'm just returning all the ids in an array but I'm hoping to find a command to delete the element AND delete the stored asset on the server
  
  Any ideas?
  
  **Tags:** assets, modules
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-28. Score: 3 | Accepted answer
  
  I'm not sure how well it'd go with 50,000 Assets, but you should just be able to call `Craft::$app->elements->deleteElement($asset)` for each one, or `Craft::$app->elements->deleteElementById($asset->id)`, as seen in the [Elements Class Reference](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelement).
  
  > **Comments:**
  > **username_0**: Will that delete the actual file off the server as well? Seems like it would just delete the element in the database  
  > **username_2**: @username_0 it'll eventually trigger the asset element's `afterDelete` method which will take care of deleting the file: https://github.com/craftcms/cms/blob/master/src/elements/Asset.php#L1292-L1300  
  
  ---
  
  