How to remove all old versions of packages
This is translation of russian topic.
Many people knows that MODX stores a lot of old versions of packages in core/packages, so you can downgrade them at any time.
It needed not so often, but packages occupying HDD space for nothing. You can clean them manually at package manager, but it a bit tiring, so I made a simple script to clean old packages automatically.
By default script must be kept in the root of a site. It will select all installed packages except the last one, and will remove them correctly via system processor.
I strongly recommend you to run it from SSH terminal, because if you will call it via web-browser it may not be able to do all stuff in servers time limit.
As I see in the code of remove processor, it has no way to harm your system, but anyway, you doing this on your own risk!
Many people knows that MODX stores a lot of old versions of packages in core/packages, so you can downgrade them at any time.
It needed not so often, but packages occupying HDD space for nothing. You can clean them manually at package manager, but it a bit tiring, so I made a simple script to clean old packages automatically.
By default script must be kept in the root of a site. It will select all installed packages except the last one, and will remove them correctly via system processor.
I strongly recommend you to run it from SSH terminal, because if you will call it via web-browser it may not be able to do all stuff in servers time limit.
As I see in the code of remove processor, it has no way to harm your system, but anyway, you doing this on your own risk!
<?php
define('MODX_API_MODE', true);
require 'index.php';
$modx->getService('error', 'error.modError');
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->setLogTarget('ECHO');
if (!XPDO_CLI_MODE) {
echo '<pre>';
}
$c = $modx->newQuery('transport.modTransportPackage');
$c->select('package_name');
$c->groupby('package_name');
if ($c->prepare() && $c->stmt->execute()) {
while ($package = $c->stmt->fetchColumn()) {
$c2 = $modx->newQuery('transport.modTransportPackage', ['package_name' => $package]);
$c2->where(['installed:!=' => '0000-00-00 00:00:00']);
$c2->sortby('installed', 'desc');
$c2->limit(1000, 1);
$c2->select('signature');
if ($c2->prepare() && $c2->stmt->execute()) {
while ($signature = $c2->stmt->fetchColumn()) {
$res = $modx->runProcessor('workspace/packages/version/remove', ['signature' => $signature]);
if (!$res->isError()) {
echo $signature . " removed!\n";
ob_flush();
} else {
$modx->log(modX::LOG_LEVEL_ERROR, "Could not remove {$signature}:" . $res->getMessage());
}
}
}
}
}
if (!XPDO_CLI_MODE) {
echo '</pre>';
}
If you already have removed some files from core/packages, you may see errors about it. Just ignore them, script will clean old records from database anyway.