<p><strong>Update</strong>: This solution doesn't work, but no-one can explain why (maybe a Craft bug?). I'm leaving the code for reference though.</p>

<p>--</p>

<p>You could get them with a <code>craft.assets</code> element criteria model and a <code>relatedTo</code> param. Just pass a <code>'and, not 8, not 13, not 26'</code> type of string to <code>sourceElement</code>, with all element IDs listed that you want to exclude from an otherwise <code>limit: null</code> assets query.</p>

<pre><code>{% set allEntryIds = craft.entries.limit(null).ids() %}
{% set allCategoryIds = craft.categories.limit(null).ids() %}

{% set omitIds = allEntryIds|merge([allCategoryIds]) %}
{% set idsParam = 'and, not ' ~ omitIds|join(', not ') %}

{% set unusedAssets = craft.assets({
    relatedTo: {
        sourceElement: idsParam,
    },
    limit: 0,
}) %}
</code></pre>

CC BY-SA 4.0
2018-09-10T13:30:42.747Z
I get an "Array to string conversion" error on the line where you join the omitIds, is there a way around that?
Jai Sandhu
CC BY-SA 3.0
2015-11-19T11:23:50.693Z
2015-11-19T12:19:08.170Z
2015-11-19T12:19:08.170Z
carlcs
<p>With good help from @carlcs here and on Slack, we found a solution:</p>

<pre><code>{% set allEntryIds = craft.entries.limit(null).ids() %}

{% set usedAssets = craft.assets({
    relatedTo: allEntryIds,
    limit: null,
}).ids() %}
{% set unusedAssets = craft.assets({
    id: 'and, not ' ~ usedAssets|join(', not '),
    limit: null
}) %}
</code></pre>

CC BY-SA 3.0
2017-05-29T10:29:14.853Z
Keep in mind, this is for all assets not used by *entries only*, they could be used by other elements like craft commerce products.
Luke Holder
CC BY-SA 3.0
2017-05-29T11:11:01.483Z
Yes, and even other assets I discovered :)
megatrond
CC BY-SA 3.0
2015-11-19T13:03:32.770Z
2015-11-19T13:03:32.770Z
megatrond
<p>How could I go about fetching all assets that are not currently beeing used anywhere?</p>

<assets><elementcriteriamodel>
Fetch all unused assets?
# Fetch all unused assets?

(Asked by: username_0 on 2015-11-19)

How could I go about fetching all assets that are not currently beeing used anywhere?

**Tags:** assets, elementcriteriamodel

---

## 2 answers

### Answer by username_2 on 2015-11-19. Score: 7 | Accepted answer

With good help from @carlcs here and on Slack, we found a solution:
{% set allEntryIds = craft.entries.limit(null).ids() %}

{% set usedAssets = craft.assets({
    relatedTo: allEntryIds,
    limit: null,
}).ids() %}
{% set unusedAssets = craft.assets({
    id: 'and, not ' ~ usedAssets|join(', not '),
    limit: null
}) %}

> **Comments:**
> **username_3**: Keep in mind, this is for all assets not used by *entries only*, they could be used by other elements like craft commerce products.  
> **username_2**: Yes, and even other assets I discovered :)  

---

### Answer by username_0 on 2015-11-19. Score: 1

Update: This solution doesn't work, but no-one can explain why (maybe a Craft bug?). I'm leaving the code for reference though.
--
You could get them with a craft.assets element criteria model and a relatedTo param. Just pass a 'and, not 8, not 13, not 26' type of string to sourceElement, with all element IDs listed that you want to exclude from an otherwise limit: null assets query.
{% set allEntryIds = craft.entries.limit(null).ids() %}
{% set allCategoryIds = craft.categories.limit(null).ids() %}

{% set omitIds = allEntryIds|merge([allCategoryIds]) %}
{% set idsParam = 'and, not ' ~ omitIds|join(', not ') %}

{% set unusedAssets = craft.assets({
    relatedTo: {
        sourceElement: idsParam,
    },
    limit: 0,
}) %}

> **Comments:**
> **username_1**: I get an "Array to string conversion" error on the line where you join the omitIds, is there a way around that?  

---

