Summer time, clean your Yunohost VPS

Summer time …It’s time to keep my YunoHost server efficient,
I remove old kernels, orphaned packages, and leftover config files. This frees up disk space and helps maintain system stability and performance.

C’est l’été, j’ai profité de faire un bon nettoyage de l’ensemble de mon instance Yunohost. après plus de 12 ans d’usage et reinstalles complètes quelques anciens kernel et anciens parametres d’Apps… cela rend le système plus stable et tout frais.
Voici un script complet après diverses sources sur le net. ( Script passé sur IA)

#!/bin/bash

# === Colors ===
BLUE="\\033[1;34m"
YELLOW="\\033[1;33m"
GREEN="\\033[1;32m"
RED="\\033[1;31m"
RESET="\\033[0m"

echo -e "${BLUE}============================"
echo "YunoHost Summer Cleanup LOGS"
echo "Command yunohost tools basic-space-cleanup"
echo -e "============================${RESET}"

echo -e "${YELLOW}Running yunohost basic-space-cleanup...${RESET}"
yunohost tools basic-space-cleanup

echo -e "${BLUE}============================"
echo "Cleaning old Linux kernels"
echo -e "============================${RESET}"

CURRENT_KERNEL=$(uname -r)
echo -e "${YELLOW}Current kernel: $CURRENT_KERNEL${RESET}"
echo -e "${YELLOW}Removing obsolete kernels...${RESET}"

dpkg --list | grep 'linux-image-[0-9]' | awk '{print $2}' | grep -v "$CURRENT_KERNEL" | xargs sudo apt remove -y

echo -e "${BLUE}============================"
echo "Cleaning orphaned packages"
echo -e "============================${RESET}"

sudo apt autoremove --purge -y
sudo apt autoclean -y
sudo apt clean

echo -e "${BLUE}============================"
echo "Purging packages with residual config"
echo -e "============================${RESET}"

sudo dpkg -l | grep '^rc' | awk '{print $2}' | xargs sudo apt purge -y

echo -e "${BLUE}============================"
echo "Removing obsolete config files"
echo -e "============================${RESET}"

sudo find /etc -name "*.dpkg-old" -delete
sudo find /etc -name "*.dpkg-dist" -delete
sudo find /etc -name "*.ucf-old" -delete

echo -e "${BLUE}============================"
echo "Cleaning unused Node.js modules"
echo -e "============================${RESET}"

echo -e "${YELLOW}⚠️  This will delete ALL node_modules folders in /var/www.${RESET}"
echo -e "${YELLOW}✔️  Safe only if the apps are uninstalled or you're planning to reinstall their dependencies.${RESET}"

read -p "Do you want to proceed with deleting all node_modules? (y/N): " confirm_node

if [[ "$confirm_node" =~ ^[Yy]$ ]]; then
    sudo find /var/www -type d -name "node_modules" -exec rm -rf {} +
    echo -e "${GREEN}✅ Node.js modules deleted.${RESET}"
else
    echo -e "${RED}⏩ Skipped deletion of node_modules.${RESET}"
fi

echo -e "${BLUE}============================"
echo "Disk space check"
echo -e "============================${RESET}"

df -h

echo -e "${GREEN}✅ Cleanup completed successfully.${RESET}"

How to use:

1/ Create the script file on your server:

nano yunohost-cleanup.sh

2/ Paste the code above, save and exit.

crtl O , Enter , crtl X

3/ Make it executable:

chmod +x yunohost-cleanup.sh

4/ Run the cleanup:

sudo ./yunohost-cleanup.sh

NB: :orange_circle: Use with caution:
:warning: This will delete all Node.js dependencies (node_modules directories) from every app located in /var/www.
:warning: Peertube, Nextcloud plugins, SearxNG, etc.
:check_mark: It is safe only if the apps have been uninstalled, or if you plan to reinstall or rebuild their dependencies.

2 Likes

I think it’s better to use yunohost tools basic-space-cleanup

3 Likes

arffff you right but is it enough?

Make a full backup
Reinstall yunohost
Don’t run post install
Restore the full backup
:winking_face_with_tongue:

1 Like

Seriously, the thing that needs to be cleaned up is all the unused Node.js

In the script now :wink:

I am not against using ai to ease work and allow to concentrate on decisions, but it’s important to understand what it provides so it serves objectives instead of introducing inconvenience, leading to the opposite of the intended purpose.
In yunohost, when uninstalling an app, all the $install_dir and the dependencies are removed.
What I was talking about is elsewhere ( in
/opt/node_n/n/versions and /usr/local/n/versions)

Edit : here is an example of uncleaned node versions

You can find the Node.js versions used by installed apps using

grep -rne "nodejs_version" /etc/yunohost/apps/

You will get versions like 18, 20
As you noticed above, versions on disk are 18.xx.xx etc…

Hope this helps

2 Likes