Programatically deleting an Asset and its related data in AEM | Community
Skip to main content
Level 3
September 24, 2023
Solved

Programatically deleting an Asset and its related data in AEM

  • September 24, 2023
  • 3 replies
  • 1803 views

Hi Everyone,

 

I have a requirement to delete an Asset from AEM through servlet.

I'm using below method to delete Asset.

 

Resource assetRes = resourceResolver.getResource(assetPath);

Node assetNode =  assetRes.adaptTo(Node.class)

assetNode = assetNode.remove();

resourceResolver.commit();

 

My question is does assetNode.remove() will remove the asset and its related data like all child nodes, version, history from CRX?

 

Also came accross removeRecursive(Node n, int saveEveryHowManyNodes) what does this method do.?

 

Thanks,

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

 

assetNode.remove() will remove the asset and its child nodes.

 

Regarding versions: 

They would not be deleted by assetNode.remove() API

  1. The versions would be deleted by version purge maintenance, as per the configurations on your system.
  2. You can also trigger version purge for a specific content path via http://localhost:4505/etc/versioning/purge.html
  3. For purging versions programmatically, one can use https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/VersionManager.html
    • It has API like purgeVersions(Node node, boolean dryRun, int maxVersions, int maxAge)
      Returns a list of purge infos of the versions that are purged using the specified conditions.
    • Version Manager can be retreived via 
        versionManager = node.getSession().getWorkspace().getVersionManager();
      

 

 

 

 

 

3 replies

Ravi_Pampana
Community Advisor
Community Advisor
September 24, 2023

Hi,

When we call node.remove() on a node in AEM, it will remove the specified node and its properties, but it does not automatically remove all child nodes, versions, or history related to that node. The removal of child nodes and related data is not done automatically because it may have significant implications on your content structure and history.

 

Regarding removeRecursive

Removes Node n and its children recursively, saving every N nodes as specified (to avoid growing the transient space too much)
Returns:
number of nodes deleted
Throws:
RepositoryException
Level 3
September 25, 2023

@ravi_pampana  I want to delete the Asset from AEM programmatically. All the child nodes, history, versions from CRX. Basically the goal to save space and improve performance is the goal.In this case how should we do it?

 

Can you please if share if you have any references for this.

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
September 25, 2023

Hello @girishb83316758 

 

assetNode.remove() will remove the asset and its child nodes.

 

Regarding versions: 

They would not be deleted by assetNode.remove() API

  1. The versions would be deleted by version purge maintenance, as per the configurations on your system.
  2. You can also trigger version purge for a specific content path via http://localhost:4505/etc/versioning/purge.html
  3. For purging versions programmatically, one can use https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/VersionManager.html
    • It has API like purgeVersions(Node node, boolean dryRun, int maxVersions, int maxAge)
      Returns a list of purge infos of the versions that are purged using the specified conditions.
    • Version Manager can be retreived via 
        versionManager = node.getSession().getWorkspace().getVersionManager();
      

 

 

 

 

 

Aanchal Sikka
kautuk_sahni
Community Manager
Community Manager
September 25, 2023

@girishb83316758 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
Level 3
September 25, 2023

Suggestions provided was helpful.

Thanks