code for Version Purging | Community
Skip to main content
Sh1ju
New Participant
January 30, 2016
Solved

code for Version Purging

  • January 30, 2016
  • 14 replies
  • 4651 views

Can you please share the code for Version Purging  using  VersionManager (com.day.cq.wcm.api)

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 Kunal_Gaba_

Sh1ju wrote...

@ Jitendera ,Its already versionable.please check the screen shot

 

 

The versionable mixin is applied on jcr:content node whereas in your code you are trying to do the operation on mm1 node which may not have the mixin. 

14 replies

OldOldOld
New Participant
February 26, 2016

The wemblog code removes all newer versions except the base version, which is probably not what you want (keep the latest version only).

To get the code:

  • search for VersionManagerImpl in CRXDE, this will give you a path like /libs/wcm/core/install/cq-wcm-core-5.6.24.jar
  • create a package in package manager with that path, build it, download it, unzip it and extract the jar with "jar xf cq-wcm-core-5.6.24.jar"
  • download a Java "disassembler" like http://jd.benow.ca/
  • open the VersionManagerImpl.class file
  • look at the "purgeVersions" method to see how its done
Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
February 5, 2016

Sh1ju wrote...

@ Jitendera ,Its already versionable.please check the screen shot

 

 

The versionable mixin is applied on jcr:content node whereas in your code you are trying to do the operation on mm1 node which may not have the mixin. 

Jitendra_S_Toma
New Participant
February 5, 2016

Hi,

If you are using AEM 6.0/6.1, here is another tool which could help you purging versions.

http://localhost:4502/etc/versioning/purge.html

Sh1ju wrote...

Can you please share the code for Version Purging  using  VersionManager (com.day.cq.wcm.api)

 

Jitendra_S_Toma
New Participant
February 5, 2016

Hi,

Have you tried purging versions using OOTB service?.

Day CQ WCM Version Purge Task

Jitendra

Jitendra_S_Toma
New Participant
February 4, 2016

Give me some time. Will get back to you very soon. Trying to validate your code.

Jitendra

Sh1ju
Sh1juAuthor
New Participant
February 4, 2016

@Jitendra please find the code

public void clearOldVersions(Session session,String nodePath,Logger logger)
{
     VersionManager versionManager = null;
     VersionHistory versionHistory = null;
     VersionIterator versionItr = null;
     
     logger.debug("Starting clearOldVersions..");
    try {
        versionManager = session.getWorkspace().getVersionManager();
    } catch (UnsupportedRepositoryOperationException e)
    {
        logger.error("UnsupportedRepositoryOperationException",e);
        
    } catch (RepositoryException e) {
        logger.error("RepositoryException",e);
    }
     
    try {
        versionHistory = versionManager.getVersionHistory(nodePath);
    } catch (UnsupportedRepositoryOperationException e) {
        logger.error("UnsupportedRepositoryOperationException",e);
    } catch (RepositoryException e) {
        logger.error("RepositoryException",e);
    }
  
     // below return all versions in order of creation date
    
    try {
        versionItr = versionHistory.getAllVersions();
    } catch (RepositoryException e) {
        logger.error("RepositoryException",e);
    }

     //Since we know MaxVersion number then we need to iterate it accordingly. 

     //  get the names of the versions
     List<String> names = new LinkedList<String>();
    
     while (versionItr.hasNext()) {
        Version v = versionItr.nextVersion();
        try {
            if (!v.getName().equals("jcr:rootVersion")) {
               versionHistory.removeVersion(v.getName());
               logger.debug("Removing the version of "+nodePath);
            }
        } catch (AccessDeniedException e) {
            logger.error("AccessDeniedException",e);
        } catch (ReferentialIntegrityException e) {
            logger.error("ReferentialIntegrityException",e);
        } catch (UnsupportedRepositoryOperationException e) {
            logger.error("UnsupportedRepositoryOperationException",e);
        } catch (VersionException e) {
            logger.error("VersionException",e);
        } catch (RepositoryException e) {
            logger.error("RepositoryException",e);
        }
     }
     
     logger.debug("clearOldVersions....completed");
}

Jitendra_S_Toma
New Participant
February 4, 2016

I didn't notice at first glance, But we are doing purging here. How does your code throws an exception which is more like "Not able to create version on a particular page."

Are you sure this exception is related to purging?.

Jitendra

Jitendra_S_Toma
New Participant
February 4, 2016

Do you mind sharing your Version Purging code with me?. 

Jitendra

Sh1ju wrote...

@ Jitendera ,Its already versionable.please check the screen shot

 

 

Sh1ju
Sh1juAuthor
New Participant
February 4, 2016

@ Jitendera ,Its already versionable.please check the screen shot

 

Jitendra_S_Toma
New Participant
February 4, 2016

Have you enable this node to make versionable?. Just verify whether it has a property (mix:versionable ) for version. If not, please add and then try it.

Ideally, this property should be at template level so that you don't need to add manually at individual pages.

Jitendra