How to get unused Components list in aem by using existing groovy script console on aem local instance | Community
Skip to main content
New Participant
October 13, 2023
Solved

How to get unused Components list in aem by using existing groovy script console on aem local instance

  • October 13, 2023
  • 3 replies
  • 1358 views

I have existing script created as a base to get a list of all components that currently are being used under /content/<myProject>/en.

 

To execute the script, I build and installed the groovy script console in my local aem instance.

 

I need a solution for to get a list of all components under /apps/<myProject>/components. And then to get seperate list for unused Components.

 

Please give me a better solution to get result.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

Hello @gvk19694 

 

You already have the list of all used components.

List of all components can easily be generated

 

Now to find the list of all components not in use:

List<Integer> unusedComponents= new ArrayList<>(allComponents); unusedComponents.removeAll(usedComponents);

Refernce: https://www.baeldung.com/java-lists-difference 

 

Assure that eaither all component paths should have full path from '/apps', else just remove it from all Strings

3 replies

kautuk_sahni
Employee
October 13, 2023

@gvk19694  Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
lukasz-m
New Participant
October 13, 2023

Hi @gvk19694,

You can use script like below. It's creating 2 lists on is including all the components form given path, and send that only contains unused components in it. At the end both list are printed.

Of course you have to set proper paths in the script replacing /content/<myProject>/en and /apps/<myProject>/components with real paths.

import javax.jcr.query.Query import com.day.cq.wcm.api.components.Component def allComponentsList = [] def unsedComponentsList = [] // getting list of all components resourceResolver.findResources("SELECT * FROM [cq:Component] AS s WHERE ISDESCENDANTNODE([/apps/<myProject>/components])", Query.JCR_SQL2).each { resource -> def component = resource.adaptTo(Component.class) allComponentsList.add(component) } // getting list of all unused components allComponentsList.each { component -> def resourceType = component.getResourceType() def numberOfComponentUsage = resourceResolver.findResources(""" SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content/<myProject>/en]) AND s.[sling:resourceType] = '$resourceType'""", Query.JCR_SQL2).size() if (numberOfComponentUsage == 0) { unsedComponentsList.add(component) } } println "All components" allComponentsList.each {cmp -> println "Component title: $cmp.title, resource type: $cmp.resourceType"} println "Unused components" println unsedComponentsList.each {cmp -> println "Component title: $cmp.title, resource type: $cmp.resourceType"}
aanchal-sikka
aanchal-sikkaAccepted solution
New Participant
October 13, 2023

Hello @gvk19694 

 

You already have the list of all used components.

List of all components can easily be generated

 

Now to find the list of all components not in use:

List<Integer> unusedComponents= new ArrayList<>(allComponents); unusedComponents.removeAll(usedComponents);

Refernce: https://www.baeldung.com/java-lists-difference 

 

Assure that eaither all component paths should have full path from '/apps', else just remove it from all Strings

Aanchal Sikka