Delete Pages in AEM using Node/Page API | Community
Skip to main content
New Participant
February 2, 2016
Solved

Delete Pages in AEM using Node/Page API

  • February 2, 2016
  • 8 replies
  • 5823 views

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!

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 smacdonald2008

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/

8 replies

Lokesh_Shivalingaiah
New Participant
February 3, 2016

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

Jitendra_S_Toma
New Participant
February 3, 2016

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

New Participant
February 3, 2016
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..!!
Naidu_Jakkana
New Participant
February 2, 2016

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.

joerghoh
Employee
February 2, 2016

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

Lokesh_Shivalingaiah
New Participant
February 2, 2016

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

smacdonald2008
smacdonald2008Accepted solution
New Participant
February 2, 2016

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/

Jitendra_S_Toma
New Participant
February 2, 2016

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