Deleting Old Versions and Log Entries

Old archived versions of files often use up a lot of space and might reduce the performance of the CMS. By means of the following script these versions can be deleted. Furthermore, the script removes the log entries associated with the deleted versions.

Please store the script in a file named cutHistory.tcl. Afterwards, you can read it into the CMS by means of the Tcl client and execute the cutHistory procedure:

CM>source /path/to/cutHistory.tcl
CM>cutHistory File_ID Remaining_Entries

Here is the script:

# Modified: 2010-04-13
# cutHistory.tcl for Fiona 6.x
#
# Purpose:
# Reduce (cut) the number of archived (i.e. previously released) versions 
# to the given number. Furthermore, remove the associated log enries.
#
# Parameters:
# - File ID or path
# - Number of released versions to remain in the system

proc cutHistory {id cutLength} {
    set objectId [findObjectId $id]
    # CMS Fiona 6.5 or later: exclude mirrors
    if {[obj withId $objectId get isMirror] eq "1"} {
        puts "\nMirror files ($objectId) don't have versions..."
    } else {
        set contents [lsort -integer -decreasing \
            [obj withId $objectId get archivedContentIds]]
        set versionLength [llength $contents]
        set logLength [llength [logEntry where logTypes action_release_obj \
            objectId $objectId]]
        puts "Cut by Length:\t$cutLength"
        puts "Log-Size:\t$logLength"
        puts "Versions:\t$versionLength"
        if {[catch {set cutByDate [content withId [lindex $contents $cutLength] \
            get lastChanged]}]} {
            puts "\nFile $objectId has no archived versions..."
        } else {
            foreach i [lrange $contents $cutLength $versionLength] {
                content withId $i delete
                puts "content $i deleted"
            }
            if {[catch {logEntry deleteWhere objectId $objectId \
                    untilDate $cutByDate} msg]} {
                puts "\nCouldn't delete log entries of file $objectId: \n$msg"
            }
        }
    }
}