System Maintenance

Regular maintenance keeps an Arch system healthy and prevents issues during updates.

Regular Maintenance Checklist

Weekly

# Update system
sudo pacman -Syu

# Check for failed services
systemctl --failed

# Review journal errors
journalctl -p 3 -xb

Monthly

# Clean package cache
sudo paccache -rk2

# Remove orphaned packages
sudo pacman -Rns $(pacman -Qtdq) 2>/dev/null || echo "No orphans"

# Check for .pacnew/.pacsave files
sudo find /etc -name "*.pacnew" -o -name "*.pacsave" 2>/dev/null

# Update mirrorlist
sudo reflector --country US --age 12 --protocol https --sort rate --save /etc/pacman.d/mirrorlist

Package Cache Management

View Cache Size

du -sh /var/cache/pacman/pkg/

Clean with paccache

Install pacman-contrib for paccache:

sudo pacman -S pacman-contrib

# Keep last 3 versions (default)
sudo paccache -r

# Keep last 2 versions
sudo paccache -rk2

# Remove all uninstalled packages
sudo paccache -ruk0

# Dry run
paccache -d

Automatic Cache Cleaning

Enable weekly cache cleaning:

sudo systemctl enable --now paccache.timer

Orphaned Packages

Find Orphans

# List orphaned packages
pacman -Qtdq

# Count orphans
pacman -Qtdq | wc -l

Remove Orphans

# Remove all orphans
sudo pacman -Rns $(pacman -Qtdq)

# If none exist, the command fails - use this instead
pacman -Qtdq | sudo pacman -Rns - 2>/dev/null || echo "No orphans found"

Configuration File Management

.pacnew and .pacsave Files

When pacman updates a package with modified config: * .pacnew - New default config (your changes preserved) * .pacsave - Your old config (new defaults applied)

# Find all .pacnew files
sudo find /etc -name "*.pacnew" 2>/dev/null

# Compare and merge
sudo diff /etc/somefile /etc/somefile.pacnew
sudo vim /etc/somefile  # Merge changes
sudo rm /etc/somefile.pacnew

Using pacdiff

# Install pacman-contrib
sudo pacman -S pacman-contrib

# Interactive merge tool
sudo pacdiff

# Use vimdiff for comparison
sudo DIFFPROG=vimdiff pacdiff

System Health Checks

Check for Issues

# Failed systemd services
systemctl --failed

# Recent errors in journal
journalctl -p err -b

# Errors since last boot
journalctl -p 3 -xb

# Disk space
df -h

Verify Installed Packages

# Check for corrupted files
sudo pacman -Qk 2>&1 | grep -v "0 missing files"

# Verify specific package
pacman -Qkk package

# Check database consistency
sudo pacman -Dk

Check Disk Usage

# Overall disk usage
df -h

# Largest directories
du -sh /* 2>/dev/null | sort -h | tail -10

# Package cache size
du -sh /var/cache/pacman/pkg/

# Journal size
journalctl --disk-usage

Journal Maintenance

View Journal Size

journalctl --disk-usage

Clean Journal

# Keep only last 2 weeks
sudo journalctl --vacuum-time=2weeks

# Limit to 500MB
sudo journalctl --vacuum-size=500M

# Keep only last 100 entries
sudo journalctl --vacuum-files=100

Persistent Configuration

Edit /etc/systemd/journald.conf:

[Journal]
SystemMaxUse=500M
SystemMaxFileSize=50M
MaxRetentionSec=1month

Then restart:

sudo systemctl restart systemd-journald

Broken System Recovery

Boot from USB

If system won’t boot:

# Boot from Arch USB

# Mount system
mount /dev/sdX2 /mnt
mount /dev/sdX1 /mnt/boot

# Chroot
arch-chroot /mnt

# Fix issues, then
exit
umount -R /mnt
reboot

Reinstall All Packages

# Reinstall all native packages
sudo pacman -Qqn | sudo pacman -S -

# Reinstall specific package
sudo pacman -S package

Downgrade Package

# From cache
sudo pacman -U /var/cache/pacman/pkg/package-oldversion.pkg.tar.zst

# Using downgrade tool
yay -S downgrade
sudo downgrade package

Automation Script

Create a maintenance script at ~/bin/arch-maintenance:

#!/bin/bash
set -e

echo "=== Arch Linux Maintenance ==="

echo -e "\n[1/6] Updating system..."
sudo pacman -Syu

echo -e "\n[2/6] Cleaning package cache..."
sudo paccache -rk2
sudo paccache -ruk0

echo -e "\n[3/6] Removing orphans..."
orphans=$(pacman -Qtdq 2>/dev/null) || true
if [[ -n "$orphans" ]]; then
    echo "$orphans" | sudo pacman -Rns -
else
    echo "No orphans found"
fi

echo -e "\n[4/6] Checking failed services..."
systemctl --failed

echo -e "\n[5/6] Checking for .pacnew files..."
sudo find /etc -name "*.pacnew" 2>/dev/null || echo "None found"

echo -e "\n[6/6] Cleaning journal..."
sudo journalctl --vacuum-time=2weeks

echo -e "\n=== Maintenance Complete ==="

Make executable:

chmod +x ~/bin/arch-maintenance