Blog» Version history limit in eZ Publish 5

Version history limit in eZ Publish 5

By Thiago Campos Viana  | August 08, 2014  |  eZ Publish development tips

Whenever you edit content in the eZ Publish Administration Interface, eZ Publish stores a new version. eZ Publish has a built-in feature to limit the number of previous versions it stores. However, as of the time of this posting, the eZ Publish 5.x public API to create and update content does not respect the version history limit (which, by default, stores 10 previous versions). We've written a quick function that restores this feature.

If you use the eZ Publish 5.x public API to update content with a script, you will end up with an unlimited number of versions. Storing previous versions you an audit log, enables you to revert to previous versions, and more. Due to storage limits and performance considerations, you don't necessarily want to store all previous versions forever.

The function below is based on the script in ezpublish_legacy/bin/php/cleanupversions.php and makes use of the legacy closure feature. Simply call this function after you've run the update and publish functions of the public API and it will handle the removal of older versions.

 * Check and removes versions according to the content version history limit
 * Use this after $contentService->publishVersion($contentDraftUpdated->versionInfo);
 * @param integer $contentId The content ID we are going to clean up versions
 * @return boolean
public function cleanUpVersions( $contentId )
    // We are going to call eZ publish legacy code by calling the legacy runCallBack function
    // For Commands
    // $legacyKernelClosure = $this->getContainer()->get( 'ezpublish_legacy.kernel' );
    // $legacyKernel = $legacyKernelClosure();
    // return $legacyKernel->runCallback( ....
    // The legacy runCallBack receives two params
    // $legacyKernel->runCallback->runCallback( [all the code], false );
    // the second parameter makes it not re-initialize the kernel every time
    return $this->getLegacyKernel()->runCallback(
        function () use ( $contentId )
            // Get eZ publish DB instance
            $db = \eZDB::instance();
            // Fetch the object by its ID
            $object = \eZContentObject::fetch( $contentId );
            // Get the total number of versions
            $versionCount = $object->getVersionCount();
            // Get the version limit, so this object should have up to this number of versions
            $versionLimit = \eZContentClass::versionHistoryLimit( $object->attribute( 'content_class' ) );
            // Check if the number of versions is lower than the max number of versions
            // If it is lower, we don't need to remove any existing version
            if ( $versionCount <= $versionLimit )
                // Nothing to do on object #{$object->attribute( 'id' )}
                return true;
            // Get the number of versions to remove
            $versionToRemove = $versionCount - $versionLimit;
            // Get the versions that should be removed
            // offset is 0 and limit is the number of versions to remove
            // so we are going to remove all the returned versions
            $versions = $object->versions( true, array(
                'conditions' => array( 'status' => \eZContentObjectVersion::STATUS_ARCHIVED ),
                'sort' => array( 'modified' => 'asc' ),
                'limit' => array( 'limit' => $versionToRemove, 'offset' => 0 ),
            ) );
            // Loop the versions array, removing all versions
            foreach( $versions as $version )
            return true;
        }, false

Join our mailing list for news, events, tips, and tools

Related Blog Posts

Creating and executing eZ Publish tasks with Mugo Queue

The Mugo Queue extension is a generic framework for queuing specific tasks in eZ Publish. It stores tasks in the ezpending_actions table and can execute...

Read more »


blog comments powered by Disqus

Hi, we're Mugo Web - Nice to meet you!

We're a group of web experts who solve complex web problems.

Learn more about us »



Yes - we can do that.

Many years of experience with complex websites allows us to offer total solutions.

Learn more about what we can do »

We love our clients (and they love us too)

We've solved problems across North America and around the world.

Learn more about what we've done »

We tweet too

Follow us on Twitter for the latest Mugo happenings

mugo twitter page @mugo

© 2008 - 2019 Mugo Web. All rights reserved.