What It Does
The script works through five places that fill a Linux server without anyone noticing, and measures free space on / and /var before and after. Each one is skipped, and says so, when there is nothing to do. The systemd journal is rotated and vacuumed when it is over JOURNAL_MAX_MB. That can delete archived journal files. An active journal file can keep the journal over the target, in which case the script warns. The package download cache is cleared with apt-get clean or dnf clean packages.
Packages nothing needs any more are removed through the package manager's own autoremove, which includes old kernels. The package manager keeps the running kernel and the newest ones, and packages you installed by hand are never candidates. Set REMOVE_UNUSED_PACKAGES to false to leave this out.
The rotated-file cleanup selects files on the /var/log filesystem by name and modification age. It recognizes compression and .old suffixes, one- or two-digit rotation suffixes, and eight-digit date suffixes. This pass skips /var/log/journal and /var/log/mspoutpost. It does not check whether a matching file is still open. Separately, it warns about up to five files over 1 GiB outside the journal directory; the warning does not exempt a file from the name-and-age check.
Docker is left alone unless you set DOCKER_PRUNE to true. Then dangling images and the build cache are pruned, and containers, volumes and images in use are never touched.
Before You Run It
- Run it with DRY_RUN=true first to inspect the cleanup plan. It still resets the package-manager log but runs none of the cleanup steps.
- Check the Docker host's own routine before you turn on DOCKER_PRUNE.
- Autoremove can remove old kernels. If a server must keep a specific package, mark it as manually installed first, or set REMOVE_UNUSED_PACKAGES to false.
- It was run on Ubuntu 24.04 with apt. The dnf path (Debian 12, RHEL, Rocky and Alma) is written for but not yet run, and neither is the Docker path, so try those on one server first.
- If the disk fills again, look for what is consuming the space. This script only clears what is already there.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: Low. Set DRY_RUN=true to list every change without making any.
- What it changes: Vacuums the journal, clears the package cache, autoremoves unused packages and old kernels, deletes old rotated logs, optionally prunes dangling Docker images and build cache.
- Restart: It never restarts the machine.
- Typical run: Under 2 minutes.
- Example answer: "Freed 410 MB (journal 120 MB, package cache 80 MB, 4 unused packages, 14 old logs 210 MB); / 61% free"
- Tested before listing on Ubuntu 24.04 (Arm64 VM) with bash, run as root, against its own test cases.
Inputs
Pass an input as an argument (--input-name value), as an environment variable named as listed, which is how most RMMs pass script variables, or change its default in the DEFAULT_ lines near the top of the script. An argument wins over an environment variable, which wins over the default.
- LOG_AGE_DAYS: Delete rotated logs not modified for this many days. 1-3650. Default 30.
- JOURNAL_MAX_MB: Vacuum the journal down to this size. 10-1000000, or 0 to leave it alone. Default 500.
- CLEAN_PACKAGE_CACHE: true to clear the downloaded package files. Default true.
- REMOVE_UNUSED_PACKAGES: true to autoremove unused packages and old kernels. Default true.
- DOCKER_PRUNE: true to prune dangling Docker images and build cache. Default false.
Every script also takes DRY_RUN, OUTPUT_FORMAT (Text, Answer, Json or Csv), EXPORT_PATH and RESULT_FIELD, which writes the answer to an RMM custom field, unless the list above words one differently.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.