How do I delete an asset with module or plugin 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()-&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);


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?
I'm not sure how well it'd go with 50,000 Assets, but you should just be able to call Craft::$app-&gt;elements-&gt;deleteElement($asset) for each one, or Craft::$app-&gt;elements-&gt;deleteElementById($asset-&gt;id), as seen in the Elements Class Reference.