public static function deleteAllButLive()
    {
        $query = new SQLSelect();
        $query->setFrom('SiteTree_Versions');

        $versionsToDelete = [];

        $results = $query->execute();

        foreach ($results as $row) {
            $ID = $row['ID'];
            $RecordID = $row['RecordID'];
            $Version = $row['Version'];
            $ClassName = $row['ClassName'];

            // is record is live?
            $query = new SQLSelect();
            $query->setSelect('Count(*) as Count');
            $query->setFrom('SiteTree_Live');
            $query->addWhere('ID = ' . $RecordID);
            $query->addWhere('Version = ' . $Version);

            $is_live = $query->execute()->value();

            if (!$is_live) {
                array_push($versionsToDelete, [
                    'RecordID'  => $RecordID,
                    'Version'   => $Version,
                    'ClassName' => $ClassName,
                ]);
            }
        }

        $affected_rows = 0;

        if (count($versionsToDelete) > 0) {
            $table_list = DB::table_list();
            $affected_tables = [];
            $doschema = new DataObjectSchema();
            foreach ($versionsToDelete as $d) {
                $subclasses = ClassInfo::dataClassesFor($d['ClassName']);
                foreach ($subclasses as $subclass) {
                    $table_name = strtolower($doschema->tableName($subclass));
                    if (!empty($table_list[$table_name . '_versions'])) {
                        DB::query('DELETE FROM "' . $table_list[$table_name . '_versions'] . '" WHERE
							"RecordID" = ' . $d['RecordID'] . ' AND "Version" = ' . $d['Version']);
                        $affected_rows = $affected_rows + DB::affected_rows();
                    }
                }
            }
        }

        return $affected_rows;
    }
public static function deleteAllButLive()
    {
        $query = new SQLSelect();
        $query->setFrom('SiteTree_Versions');

        $versionsToDelete = [];

        $results = $query->execute();

        foreach ($results as $row) {
            $ID = $row['ID'];
            $RecordID = $row['RecordID'];
            $Version = $row['Version'];
            $ClassName = $row['ClassName'];

            // is record is live?
            $query = new SQLSelect();
            $query->setSelect('Count(*) as Count');
            $query->setFrom('SiteTree_Live');
            $query->addWhere('ID = ' . $RecordID);
            $query->addWhere('Version = ' . $Version);

            $is_live = $query->execute()->value();

            if (!$is_live) {
                array_push($versionsToDelete, [
                    'RecordID'  => $RecordID,
                    'Version'   => $Version,
                    'ClassName' => $ClassName,
                ]);
            }
        }

        $affected_rows = 0;

        if (count($versionsToDelete) > 0) {
            $table_list = DB::table_list();
            $affected_tables = [];
            $doschema = new DataObjectSchema();
            foreach ($versionsToDelete as $d) {
                $subclasses = ClassInfo::dataClassesFor($d['ClassName']);
                foreach ($subclasses as $subclass) {
                    $table_name = strtolower($doschema->tableName($subclass));
                    if (!empty($table_list[$table_name . '_versions'])) {
                        DB::query('DELETE FROM "' . $table_list[$table_name . '_versions'] . '" WHERE
							"RecordID" = ' . $d['RecordID'] . ' AND "Version" = ' . $d['Version']);
                        $affected_rows = $affected_rows + DB::affected_rows();
                    }
                }
            }
        }

        return $affected_rows;
    }
public static function deleteAllButLive ( ) { $ query = new SQLSelect ( ) ; $ query -> setFrom ( 'SiteTree_Versions' ) ; $ versionsToDelete = [ ] ; $ results = $ query -> execute ( ) ; foreach ( $ results as $ row ) { $ ID = $ row [ 'ID' ] ; $ RecordID = $ row [ 'RecordID' ] ; $ Version = $ row [ 'Version' ] ; $ ClassName = $ row [ 'ClassName' ] ; // is record is live? $ query = new SQLSelect ( ) ; $ query -> setSelect ( 'Count(*) as Count' ) ; $ query -> setFrom ( 'SiteTree_Live' ) ; $ query -> addWhere ( 'ID = ' . $ RecordID ) ; $ query -> addWhere ( 'Version = ' . $ Version ) ; $ is_live = $ query -> execute ( ) -> value ( ) ; if ( ! $ is_live ) { array_push ( $ versionsToDelete , [ 'RecordID' => $ RecordID , 'Version' => $ Version , 'ClassName' => $ ClassName , ] ) ; } } $ affected_rows = 0 ; if ( count ( $ versionsToDelete ) > 0 ) { $ table_list = DB :: table_list ( ) ; $ affected_tables = [ ] ; $ doschema = new DataObjectSchema ( ) ; foreach ( $ versionsToDelete as $ d ) { $ subclasses = ClassInfo :: dataClassesFor ( $ d [ 'ClassName' ] ) ; foreach ( $ subclasses as $ subclass ) { $ table_name = strtolower ( $ doschema -> tableName ( $ subclass ) ) ; if ( ! empty ( $ table_list [ $ table_name . '_versions' ] ) ) { DB :: query ( 'DELETE FROM "' . $ table_list [ $ table_name . '_versions' ] . '" WHERE
							"RecordID" = ' . $ d [ 'RecordID' ] . ' AND "Version" = ' . $ d [ 'Version' ] ) ; $ affected_rows = $ affected_rows + DB :: affected_rows ( ) ; } } } } return $ affected_rows ; }
Remove ALL previous versions of a SiteTree record

@param  Null
@return Int
Remove ALL previous versions of a SiteTree record