<p>This should work</p>

<pre><code>//First fetch all ids from elements table
$allIds = craft()-&gt;db-&gt;createCommand()-&gt;select('id')-&gt;from('elements')-&gt;where([
    'not like', 'type', 'Asset'
])-&gt;queryColumn();

// query for all related Assets -&gt; assets that are used somewhere
$usedAssetCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset);
$usedAssetCriteria-&gt;relatedTo = $allIds;
$usedAssetCriteria-&gt;limit = null;
$usedAssets = $usedAssetCriteria-&gt;ids();

// query for all assets not in the list of the used one
$unUsedCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset);
$unUsedCriteria-&gt;id = 'and, not ' . implode(', not ', $usedAssets);
craft()-&gt;assets-&gt;deleteFiles($unUsedCriteria-&gt;ids());
</code></pre>

CC BY-SA 3.0
2017-11-07T17:54:44.070Z
Thank you, but for some reason, it returns all assets. (I replaced the delete part by a var_dump)
r-ninja
CC BY-SA 3.0
2017-11-07T18:09:22.967Z
sry, my mistake, you have to replace the `.find()` with an `.ids()` I updated my answer. It works now for all assets with no relation
Robin Schambach
CC BY-SA 3.0
2017-11-09T15:09:17.760Z
Does this also cover assets linked in redactor fields?
Jan Henckens
CC BY-SA 3.0
2017-11-09T15:15:44.343Z
No, that's only one reason why you should avoid using images in rich text fields. The advantage of craft is: you can avoid using images in those fields and instead create a matrix element with a text and image input. However, if you want include those you would have to loop through every rich text in every field (including matrix elements) and parse the images
Robin Schambach
CC BY-SA 3.0
2017-11-07T05:47:07.877Z
2017-11-07T18:09:44.657Z
2017-11-07T18:09:44.657Z
Robin Schambach
<p>The title is self-explanatory, but I need to buid a service function to remove unused assets, or at least to fetch those in order to delete found assets after.</p>

<p>Any simple solution?</p>

<plugin-development><assets><delete>
Fetch/Remove all unused assets (by plugin)
# Fetch/Remove all unused assets (by plugin)

(Asked by: username_0 on 2017-11-07)

The title is self-explanatory, but I need to buid a service function to remove unused assets, or at least to fetch those in order to delete found assets after.

Any simple solution?

**Tags:** plugin-development, assets, delete

---

## 1 answers

### Answer by username_1 on 2017-11-07. Score: 4 | Accepted answer

This should work
//First fetch all ids from elements table
$allIds = craft()->db->createCommand()->select('id')->from('elements')->where([
    'not like', 'type', 'Asset'
])->queryColumn();

// query for all related Assets -> assets that are used somewhere
$usedAssetCriteria = craft()->elements->getCriteria(ElementType::Asset);
$usedAssetCriteria->relatedTo = $allIds;
$usedAssetCriteria->limit = null;
$usedAssets = $usedAssetCriteria->ids();

// query for all assets not in the list of the used one
$unUsedCriteria = craft()->elements->getCriteria(ElementType::Asset);
$unUsedCriteria->id = 'and, not ' . implode(', not ', $usedAssets);
craft()->assets->deleteFiles($unUsedCriteria->ids());

> **Comments:**
> **username_0**: Thank you, but for some reason, it returns all assets. (I replaced the delete part by a var_dump)  
> **username_1**: sry, my mistake, you have to replace the `.find()` with an `.ids()` I updated my answer. It works now for all assets with no relation  
> **username_2**: Does this also cover assets linked in redactor fields?  
> **username_1**: No, that's only one reason why you should avoid using images in rich text fields. The advantage of craft is: you can avoid using images in those fields and instead create a matrix element with a text and image input. However, if you want include those you would have to loop through every rich text in every field (including matrix elements) and parse the images  

---

