I got requirement that to filter un-used assets present in dam
-> I have written servlet and triggered SQL-2 Query to fetch all assets from dam, I am getting the all dam assets.
My Code to fetch the assets
RowIterator rowIterator = queryResult.getRows();
while (rowIterator.hasNext()) {
Row row = rowIterator.nextRow();
//Here i am getting all the DAM assets
LOG.info("---assets--->"+row.toString());
Now i need to filter the assets that are not used in whole project.
Solved! Go to Solution.
Views
Replies
Total Likes
Hello @vineel_k -
You can leverage the OOTB Asset Usage service to check if each asset is being used within the AEM project.
Here is the detailed example of how you can you achieve the same programmatically
For your requirement, you can leverage the AssetReferenceSearch API which can give the details of Assets used in a page (node of type cq:Page).
See this discussion also.
Hello @vineel_k
We can use "ACS Commons Renovator" to find unused assets.
The following blog shares the details on how we can find, unpublish and delete unused assets
https://kiransg.com/2021/11/10/aem-asset-repo-cleanup-acs-commons-renovator/
Thank you @aanchal-sikka this worked completely fine. Is there any way I can get the dates(last modified or published) of the of the assets in the excel sheet as well?
Views
Replies
Total Likes
Hi,
You can use ReferenceSearch API to find whether the given asset is used on the site
Hello @vineel_k -
You can leverage the OOTB Asset Usage service to check if each asset is being used within the AEM project.
Here is the detailed example of how you can you achieve the same programmatically
@vineel_k You can use the OOTB feature as suggested by @Tanika02 but if you really want to from custom code follow below code and specify the particular path in "assetManager.listChildren("/content/dam");"
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service = SomeService.class, immediate = true)
public class SomeService {
@Reference
private ResourceResolverFactory resourceResolverFactory;
public void filterUnusedAssets() {
ResourceResolver resourceResolver = null;
try {
// Acquire a ResourceResolver using the ResourceResolverFactory
Map<String, Object> authInfo = new HashMap<>();
authInfo.put(ResourceResolverFactory.SUBSERVICE, "your-service-name");
resourceResolver = resourceResolverFactory.getServiceResourceResolver(authInfo);
// Get the AssetManager
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
// Get all assets under the DAM path
Iterator<Asset> assetIterator = assetManager.listChildren("/content/dam");
// Iterate through the assets and filter unused ones
while (assetIterator.hasNext()) {
Asset asset = assetIterator.next();
String assetPath = asset.getPath();
// Check if the asset is referenced
boolean isReferenced = assetManager.isAssetReferenced(assetPath);
if (!isReferenced) {
// Asset is unused, do something with it
System.out.println("Unused Asset Path: " + assetPath);
}
}
} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
} finally {
// Close the ResourceResolver
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
}
Views
Like
Replies