<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>

CC BY-SA 4.0
2018-12-28T14:25:29.993Z
Will that delete the actual file off the server as well? Seems like it would just delete the element in the database
Keith Mancuso
CC BY-SA 4.0
2018-12-28T17:16:49.573Z
@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
Brad Bell
CC BY-SA 4.0
2018-12-28T05:18:11.890Z
2018-12-28T05:18:11.890Z
Matt Stein
<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>

<assets><modules>
How do I delete an asset with module or plugin
# 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**: @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  

---

