Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Unused AEM Assets

Avatar

Level 2

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Employee

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

View solution in original post

3 Replies

Avatar

Correct answer by
Employee

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

Avatar

Level 3

@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.

 

Avatar

Level 2

Some time ago I wrote a bash script. It couldn't be more badly programmed, but it works! (I'm not a developer!)

The script also takes a while because it first pulls all the assets and then has to check each asset individually to see whether it is referenced or not.

 

https://github.com/frappierer/unrefAemAssets