Smart ZimaOS Cleanup Script (Safe for System with Backups)

Smart ZimaOS Cleanup Script (Safe for System with Backups)

This is a safe and modular cleanup script that helps free up space on ZimaOS without touching critical system files or breaking running containers.
It’s especially useful when / shows 100% full (which is normal on ZimaOS) but your /DATA drive is filling up due to backups, Docker layers, or cached files from heavy apps.

What this script does

Keeps only your newest backups (auto-removes older ones)
Clears logs and temporary cache folders
Safely prunes Docker images and volumes
Works on any ZimaOS setup
Easy to extend for your own apps

Full Script:

cat > /DATA/clean_zima_storage_safe.sh <<'EOF'
#!/bin/bash
echo "=== ZimaOS Smart Cleanup Started ==="

# --- 1. Backup rotation (keeps newest only)
if [ -d "/DATA/Backups" ]; then
    echo "Checking backups in /DATA/Backups..."
    for backup_dir in "/DATA/Backups"/*; do
        if [ -d "$backup_dir" ]; then
            echo "Processing $backup_dir..."
            cd "$backup_dir"
            folders=($(ls -1t 2>/dev/null))
            keep="${folders[0]}"
            for f in "${folders[@]:1}"; do
                echo "Removing old backup: $f"
                rm -rf "$f"
            done
            cd /
        fi
    done
else
    echo "No backup directory found, skipping..."
fi

# --- 2. Clear system logs
if [ -d "/DATA/.log" ]; then
    echo "Clearing logs..."
    rm -rf /DATA/.log/*
fi

# --- 3. App-specific cache cleanup (modular section)
# Add or remove apps here as needed
declare -A APPS_TO_CLEAN=(
    [immich]="/tmp /cache"
    [nextcloud]="/tmp /cache /data/updater-*/backups"
    [plex-nvidia]="/Library/Application Support/Plex Media Server/Cache"
    [jellyfin]="/cache /metadata/Temp"
    [netdata]="/cache"
)

for app in "${!APPS_TO_CLEAN[@]}"; do
    app_dir="/DATA/AppData/$app"
    if [ -d "$app_dir" ]; then
        echo "Cleaning cache for $app..."
        for path in ${APPS_TO_CLEAN[$app]}; do
            target="$app_dir$path"
            if [ -d "$target" ]; then
                echo "  - Removing $target"
                rm -rf "$target"/*
            fi
        done
    fi
done

# --- 4. Docker cleanup (safe mode)
echo "Running Docker cleanup..."
docker system prune -f
docker volume prune -f

echo "=== Cleanup complete. Run 'df -h /DATA' to check free space ==="
EOF
chmod +x /DATA/clean_zima_storage_safe.sh

Run manually
To execute the main cleanup script you created above, run the following command.
This command launches the script from /DATA, triggering all cleanup tasks immediately — including backup rotation, log clearing, app cache cleanup, and Docker pruning.
You can run it any time you want to safely free up space on your system.

chmod +x /DATA/clean_zima_storage_safe.sh
bash /DATA/clean_zima_storage_safe.sh

Add your own apps

To include another app, just add a new line under APPS_TO_CLEAN, for example:

[paperless-ngx]="/data/tmp /data/cache"

The script will automatically detect and clean those folders safely.

Example result

After running this on my system:

  • /DATA usage dropped from 82% to 46%
  • Freed up over 500 GB
  • System remained stable
  • The newest “my apps” backup was preserved

Safety notes

  • Only affects /DATA (never system files under /)
  • Keeps newest backups automatically
  • Skips missing folders
  • Works for ZimaOS 1.4-1.5+, including CasaOS
3 Likes

thx

Can I tell you in more detail how to use this script?

Below is a step-by-step explanation on how to use the script.
Follow it in order and copy-paste the commands exactly.

Step 1 – Check current /DATA usage (before)

df -h /DATA

Step 2 – Create the cleanup script (one-time setup)
Copy and paste the full script into this file:

nano /DATA/clean_zima_storage_safe.sh

Paste the script, save, and exit.

Step 3 – Make the script executable (one time only)

chmod +x /DATA/clean_zima_storage_safe.sh

Step 4 – Run the cleanup (only when you need space)

bash /DATA/clean_zima_storage_safe.sh

Step 5 – Check /DATA usage again (after)

df -h /DATA

What the script does

  • Keeps only the newest backup in /DATA/Backups
  • Removes old backups safely
  • Clears logs in /DATA/.log
  • Cleans cache/temp folders for listed apps under /DATA/AppData
  • Removes unused Docker images and volumes (does not stop running containers)

How to add your own apps
Inside the script, find APPS_TO_CLEAN and add an entry like this:

[paperless-ngx]="/data/tmp /data/cache"

Only add cache or temp paths, never config or main data folders.

If there’s anything you’re not sure about, just ask before running it.

When I try to copy, paste, and save the script, I get an error saying “Error writing /DATA/clean_zima_storage_safe.sh: Permission denied”. Any idea how to get around that?

I ran it as sudo and that let me write out and exit

Yes, that’s just a permissions issue.

On ZimaOS, /DATA is owned by root, so a normal user can’t write files there. You need to create and run the script with elevated privileges.

Open the file using sudo, paste the script, save it, then make it executable and run it with sudo as well.

Nothing is wrong with your system, this is expected behaviour.
If it still fails after using sudo, share the output of ls -ld /DATA and we can check further.