version purging

Do not use this code without this fix: https://issues.apache.org/jira/browse/JCR-2613 (fixed starting CRX 2.2)


Session session = resource.getResourceResolver().adaptTo(Session.class);
QueryManager qm = session.getWorkspace().getQueryManager();

String xpath = "";

xpath = "//element(*,nt:frozenNode)[jcr:like(cq:parentPath, '<some-path>%')]";

QueryResult result = qm.createQuery(xpath, javax.jcr.query.Query.XPATH).execute();
NodeIterator iter = result.getNodes();
Node node = null;

while (iter.hasNext()) {
    node = iter.nextNode();
    String path = node.getPath();
    VersionManager mgr = session.getWorkspace().getVersionManager();

// get version history
    VersionHistory vh = (VersionHistory) node.getParent().getParent();
//  VersionHistory vh = mgr.getVersionHistory(path);
    String id = vh.getIdentifier();
 
//  get the names of the versions
    List<String> names = new LinkedList<String>();
    VersionIterator vit = vh.getAllVersions();
    while (vit.hasNext()) {
        Version v = vit.nextVersion();
        if (!v.getName().equals("jcr:rootVersion")) {
           names.add(v.getName());
        }
    }
       
// remove all versions
    for (String name: names) {
      vh.removeVersion(name);
    }
}
session.logout();
Comments