Unused AEM Assets

ArchanaVA

11-02-2020

Is there any way to retrieve unused assets from DAM in AEM 6.4 without using JAVA servlets

Need to bulk delete the unused assets from DAM

AEM 6.4 Assets

Accepted Solutions (1)

Accepted Solutions (1)

nidhip010816

Employee

12-02-2020

Ways by which you could find if an asset is used or not -

 

1. Go to host:port/damadmin, select asset for the asset -> Go to Tools -> references.

2. You may use filters by going to host:port/assets.html/content/dam -> Select asset and use 'Filter' present in sidekick at left

nidhip010816_0-1581518162025.png

 

 

3. There is no OOTB functionality for this but if you are intrested in custom implementation, this post might help you ->

https://stackoverflow.com/questions/35187627/how-to-find-unused-images-from-cq-dam

 

Hope this helps!

 

~Nidhi

Answers (1)

Answers (1)

dvnSudheer

13-02-2020

@ArchanaVA, There is no direct way to find the assets which does not have any references, you have to iterate through all the assets and check it manually or via code.

 

One way without servlet is to use Groovy scripts. for groovy scripts, you don't need to deploy any code (like servlets) in the system, Instead you can write your code on fly and make the changes(Update/Delete). As there is no direct way, you have to either iterate through all assets (folder by folder) or write a query (Try to limit the number of assets at a time, it will be easy to track and better on the performance side) to get all the assets for checking the references.

Once you got the asset paths, we can iterate through and check the references for each asset. Based on the result, you can delete them.

 

Note: I see that even there are no references for the assets, sometimes the current context of groovyconsole is also coming in the references, to avoid that I have added a small logic to check the path when the count is 1. I have different groovy versions in different AEM instances, one with the latest one does not have the issue. If you are not having this issue, you can remove the lines 16 to 27 from the below markup, but safer to check once before removing the logic. 

 

I have iterated through a folder of assets, but if you can also use query to get the assets and change the logic.

 

Groovy Script:

 

import com.day.cq.wcm.commons.ReferenceSearch;
import javax.jcr.NodeIterator;

def folderNode = session.getNode('/content/dam/site/en/folder1');
def childNodes = folderNode.getNodes();

ReferenceSearch referenceSearch = new ReferenceSearch();

childNodes.each { childNode ->
    def path = childNode.path;
    def results = referenceSearch.search(resourceResolver, path);
    println results;
    println results.size();
    
    def currentReference = false;
    if(results.size() == 0) {
        currentReference = true;
    } else if(results.size() == 1) {
        results.each { reference ->
            if(reference.toString().contains('/etc/groovyconsol')) {
                currentReference = true;
            } else {
                currentReference = false;
            }
        }
    }
    if(results.size() > 1 || !currentReference) {
        def node = session.getNode(path);
        node.remove();
        println 'removed asset : ' + path;
        //Uncomment the session.save() to remove the asset node.
        //session.save();
    }
}

 

 

 

Alternatively, you can also use a direct query in the website with fileReference parameter (which we generally use for the images), but in the below sample, I have restricted my search to a specific site path. you can change it to '/content' but it might be performance impact on the whole system. It's better to use the first option using the reference API .

def buildQuery(path, assetPath) {
    def queryManager = session.workspace.queryManager;
    def statement = "/jcr:root${path}//*[@fileReference='" + assetPath + "']"
    def query = queryManager.createQuery(statement, 'xpath')
}

final def query = buildQuery('/content/site/en', '/content/dam/site/en/sample.png');
final def result = query.execute();

println result.nodes.size();
//If size is greater than 0, then we can assume the image has references.