Expand my Community achievements bar.

SOLVED

Delete Pages in AEM using Node/Page API

Avatar

Level 4

Hi,

I am trying to delete all the pages which does not have any child pages in the certain path in AEM . Can someone please tell me how i can achieve this through Node/Page API?

Thanks for your replies!

1 Accepted Solution

Avatar

Correct answer by
Level 10

Here is a good link that will give you some ideas of using JCR SQLs to query for pages. 

http://labs.6dglobal.com/blog/2014-10-07/9-jcr-sql-2-queries-every-aem-dev-should-know/

View solution in original post

8 Replies

Avatar

Level 9

You have to write a query to get all AEM pages (type of cq:page) without child pages and once you get that use JCR PageManger API to delete all of them.

JCR PageManager API : https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/PageManager.html

Jitendra

Avatar

Correct answer by
Level 10

Here is a good link that will give you some ideas of using JCR SQLs to query for pages. 

http://labs.6dglobal.com/blog/2014-10-07/9-jcr-sql-2-queries-every-aem-dev-should-know/

Avatar

Level 10

Query to get all the pages

Select * from [cq:Page] As a where isdescendantnode (s,'/content')

once you have the list of pages, loop through the list of Nodes and check 

if (node.hasNodes()) and run the above query again with the current resourcepath as the path

Select * from [cq:Page] As a where isdescendantnode (s,'<resourcePath>')

if the resultlist is > 0, then it would have the child page nodes and then use the PageManager API to delete the same

import com.day.cq.wcm.api.PageManager; ResourceResolver resolver = <Get instance of resource resolver> <If you have request object then request.getResourceResolver()> PageManager pm =  resolver.adaptTo(PageManager.class); pm.delete(Resource pagepath, boolean false, boolean true) throws WCMException

 

** This is to just give you an idea... and not the actualy code

Avatar

Employee Advisor

Hi,

given the huge amount of cq:Page nodes below /content I wouldn't do it with JCR query. Just use a recursive algorithm to find all cq:Pages with the given criteria and execute the action. Should be enough and probably faster the doing queries.

Jörg

Avatar

Level 2

You can do recursive call using node.getChildren and repeat for each child and remove if it doesnt have any children.

You can further check if it is page node and delete.

Note : To persist a removal, a save must be performed that includes the (former) parent of the removed item within its scope.

Avatar

Level 4
Hey guys thanks for your replies, all of your answers seems correct. But my requirement is little tricky, I have the child most page node and from there in need to traverse through all the ancestors of this page. And the page object has a property say "property1" and the ancestors of this page could either have a page with the same property "property1 " . So can let me know can I write a query to check if the page has property or not. I need to delete all the parents that don't have this property. Actually i came up with adding this property to have a check on the pages .That should probably resolve my issue.. Thanks a lot for your replies..!!

Avatar

Level 9

Your whole requirement is very confusing, specially " I need to delete all the parents that don't have this property. ". 

I am not sure how can you do so. However, if you want to remove all the pages with some property (e.g. property1), do the query to get a list of pages & then you can remove one by one.

Jitendra

Avatar

Level 10

yes.. it can be your logic to find which page node to delete. However, you will have to traverse using the APIs and based on your condition you can perform the actions.

Regards,

bsloki