Search code examples
jcrjackrabbit

Trimming down or capping max version history for Apache Jackrabbit JCR


In an application that uses Apache Jackrabbit as JCR backend there are some versionable nodes in the repository (mostly text content). It seems that version history for those nodes just grows "in perpetuity".

What is the best/correct way to configure max number of versions in the version history (per workspace?) - for the future history entries AND to "trim down" the existing version histories for an arbitrary max number of entries per history?


Solution

  • We just set a property, a limit on number of versions we want to keep then delete the excess.

    Here's an untested idea of how you could do it...

    public void deleteOldVersions(Session session, String absPath, int limit) 
        throws RepositoryException {
    
        VersionManager versionManager = session.getWorkspace().getVersionManager();
        VersionHistory versionHistory = versionManager.getVersionHistory(absPath);
        VersionIterator versionIterator = versionHistory.getAllLinearVersions(); // Gets the history in order
        long numberToDelete = versionIterator.getSize() - limit;
        while (numberToDelete-- > 0) {
            String versionName = versionIterator.nextVersion().getName();
            versionHistory.removeVersion(versionName);
        }
    }