<p>Create this function in your plugins variable:</p>

<pre><code>public function getAllIds(){
    return craft()-&gt;db-&gt;createCommand()-&gt;select('id')-&gt;from('elements')-&gt;where([
        'not like', 'type', 'Asset'
    ])-&gt;queryColumn();
}
</code></pre>

<p>You'll fetch all ids in Craft, then you can to this:</p>

<pre><code>{% set allIds = craft.your-plugin-handle.getAllIds() %}

{% set usedAssets = craft.assets({
    relatedTo: allIds,
    limit: null,
}).ids() %}

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

<p>To be honest I'm not sure what the error is in your code. You should get all entries related to the asset, even if they are in a matrix block. Maybe you related to asset to a category and this category to an entry and you assume to get the entry(?)</p>

CC BY-SA 3.0
2017-10-22T16:08:51.883Z
Thanks Robin, but I was trying to do it in a template rather than a plugin. I have also now realised that all of my document links are in a rich text field so not searchable anyway. I'm testing the Asset-use plugin and it looks like it's working OK, I'm just a bit put off by the Beta label.
Paul Frost
CC BY-SA 3.0
2017-10-21T16:45:59.710Z
2017-10-21T16:45:59.710Z
Robin Schambach
<p>I want to be able to delete assets that are not linked to. I have adapted the following code from previous examples of this kind of thing, but it doesn't seem to pick up all the related entries, just some of them. Some photos do not show a related entry but if I go to the entry where I think it might have been used it is still in use. Am I missing something in matching the entries to the assets?</p>

<pre><code>{% set photos = craft.assets.kind('image') %}
{% for photo in photos %}
    &lt;img src="{{ photo.getUrl('square') }}"&gt;
    &lt;br&gt;{{ photo.filename }} / {{ photo.folderId }}
    &lt;ul&gt;
        {# Loop throught entries related to this asset #}
        {% for entry in craft.entries.relatedTo(photo).order('title') %}
            &lt;li&gt;{{ entry.title }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endfor %}
</code></pre>

<p>I have tried using the <a href="https://github.com/born05/craft-assetusage" rel="nofollow noreferrer">assetusage plugin</a> but it doesn't seem to work with non images and I also wanted to test if it was working properly.
I also looked at the <a href="https://github.com/weareboxhead/Craft-Asset-Use" rel="nofollow noreferrer">Asset-Use plugin</a> but that is beta and only does images.</p>

<assets>
How to find out which assets are being used
# How to find out which assets are being used

(Asked by: username_0 on 2017-10-20)

I want to be able to delete assets that are not linked to. I have adapted the following code from previous examples of this kind of thing, but it doesn't seem to pick up all the related entries, just some of them. Some photos do not show a related entry but if I go to the entry where I think it might have been used it is still in use. Am I missing something in matching the entries to the assets?
{% set photos = craft.assets.kind('image') %}
{% for photo in photos %}
    
    {{ photo.filename }} / {{ photo.folderId }}
    
        {# Loop throught entries related to this asset #}
        {% for entry in craft.entries.relatedTo(photo).order('title') %}
            {{ entry.title }}
        {% endfor %}
    
{% endfor %}

I have tried using the assetusage plugin but it doesn't seem to work with non images and I also wanted to test if it was working properly.
I also looked at the Asset-Use plugin but that is beta and only does images.

**Tags:** assets


---

## 1 answers

### Answer by username_1 on 2017-10-21. Score: 1

Create this function in your plugins variable:
public function getAllIds(){
    return craft()->db->createCommand()->select('id')->from('elements')->where([
        'not like', 'type', 'Asset'
    ])->queryColumn();
}

You'll fetch all ids in Craft, then you can to this:
{% set allIds = craft.your-plugin-handle.getAllIds() %}

{% set usedAssets = craft.assets({
    relatedTo: allIds,
    limit: null,
}).ids() %}

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

To be honest I'm not sure what the error is in your code. You should get all entries related to the asset, even if they are in a matrix block. Maybe you related to asset to a category and this category to an entry and you assume to get the entry(?)

> **Comments:**
> **username_0**: Thanks Robin, but I was trying to do it in a template rather than a plugin. I have also now realised that all of my document links are in a rich text field so not searchable anyway. I'm testing the Asset-use plugin and it looks like it's working OK, I'm just a bit put off by the Beta label.  

---

