Find unused assets

This script generates a list of all assets which are not used in the website workspace. This script requires a dx core installation.

import info.magnolia.module.dependencies.impl.DefaultDependencies
import info.magnolia.cms.beans.config.ContentRepository
import java.text.DecimalFormat;

query = "select node.* from [mgnl:asset]";
result = QueryUtil.search("dam", query, "JCR-SQL2", "mgnl:asset");

dependencies = new DefaultDependencies(null , MgnlContext.getInstance())
def i = 0
def found = false
result.each { node ->
    found = false
    ws = "website"
    nodeType = "mgnl:page"
    if(dependencies.getReferencesTo(node, ws, nodeType).isEmpty()) {
    	found = true
    	i++;
        if (i % 20 == 0) {
    	    sleep(10)
    	}
    }

	if (found) {
	    def fNode = node.getPath()
	    if (node.hasNode('jcr:content')) {
	        println ws + " " + node.getPath()+","+new DecimalFormat("##.##").format((PropertyUtil.getLong(node.getNode('jcr:content'), 'size')/1024/1024).doubleValue())
	    }
	     
	    found = false
	}
}