DigitalOcean n8n Docker – Disk & DB Maintenance

Quick guide to reclaim space and automate disk maintenance for n8n running in Docker on DigitalOcean Droplets

Quick guide to reclaim space and automate disk maintenance for n8n running in Docker on DigitalOcean Droplets.
คู่มือด่วนสำหรับเพิ่มพื้นที่และตั้งค่าบำรุงรักษาดิสก์อัตโนมัติบน Droplet ที่รัน n8n Docker


🔧 Command Generator

เครื่องมือสร้างคำสั่ง

Generate custom commands with your specific paths and settings.
สร้างคำสั่งที่กำหนดเองด้วย path และการตั้งค่าของคุณ

ไปยังโฟลเดอร์ n8n

n8n Project Path (from find command in step 6):

/home/user/n8n

Generated Commands:

cd /home/user/n8n
docker compose down && docker compose up -d

Generate Webhook Report Script

สร้างสคริปต์รายงาน Webhook

n8n Webhook URL:

https://your-n8n-instance.com/webhook/maintenance-logs

Script Location (optional):

/root/send_logs_to_n8n.sh

Generated Script:

#!/bin/bash
# Replace YOUR_N8N_WEBHOOK_URL with your actual webhook URL
WEBHOOK_URL="https://your-n8n-instance.com/webhook/maintenance-logs"

DOCKER_LOG=$(tail -1 /var/log/docker_prune.log 2>/dev/null)
BINARY_LOG=$(tail -1 /var/log/binary_data_cleanup.log 2>/dev/null)
VACUUM_LOG=$(tail -1 /var/log/db_vacuum.log 2>/dev/null)
DISK_LOG=$(tail -1 /var/log/disk_trend.log 2>/dev/null)

curl -X POST -H "Content-Type: application/json" -d "$(jq -n \
  --arg docker "$DOCKER_LOG" \
  --arg binary "$BINARY_LOG" \
  --arg vacuum "$VACUUM_LOG" \
  --arg disk "$DISK_LOG" \
  '{docker_prune:$docker, binary_data_cleanup:$binary, db_vacuum:$vacuum, disk_trend:$disk}')" \
  "$WEBHOOK_URL"

Cron Command:

15 6 * * * /root/send_logs_to_n8n.sh

1. Check Current Disk Usage

1. ตรวจสอบพื้นที่ดิสก์ปัจจุบัน

df -h
  • Look at Use% for your main disk (/dev/vda1).
    ดูที่ Use% สำหรับดิสก์หลัก (/dev/vda1)

2. Expand Disk / Partition (Optional)

2. ขยายดิสก์ / พาร์ทิชัน (ถ้าจำเป็น)

Check new size / ตรวจสอบขนาดใหม่

lsblk

Install growpart / ติดตั้ง growpart

sudo apt update && sudo apt install cloud-guest-utils

Grow partition / ขยายพาร์ทิชัน

sudo growpart /dev/vda 1

Resize filesystem / ปรับขนาดไฟล์ระบบ

sudo resize2fs /dev/vda1

Note: Use xfs_growfs / for XFS filesystem.
หมายเหตุ: ใช้ xfs_growfs / สำหรับไฟล์ระบบ XFS


3. Manual Docker Cleanup

3. ล้าง Docker ด้วยตนเอง

docker system prune -af --volumes

⚠️ Warning: This will remove all unused containers, networks, images, and volumes.
คำเตือน: คำสั่งนี้จะลบ containers, networks, images และ volumes ที่ไม่ได้ใช้ทั้งหมด


4. Configure Daily Cron Jobs

4. ตั้งค่า Cron Jobs รายวัน

Open crontab editor / เปิดตัวแก้ไข crontab

sudo crontab -e

First time? You'll be asked to choose an editor. Select nano (usually option 1) for beginners.
ครั้งแรก? จะถูกถามให้เลือกตัวแก้ไข เลือก nano (มักจะเป็นตัวเลือก 1) สำหรับผู้เริ่มต้น

Add these jobs / เพิ่มงานเหล่านี้

0 3 * * * docker system prune -af --volumes >> /var/log/docker_prune.log 2>&1
0 4 * * * find /var/lib/docker/volumes/n8n_data/_data/binaryData -type f -mtime +7 -delete >> /var/log/binary_data_cleanup.log 2>&1
0 5 * * * sqlite3 /var/lib/docker/volumes/n8n_data/_data/database.sqlite "VACUUM;" >> /var/log/db_vacuum.log 2>&1
0 6 * * * echo "$(date +'%F %T') $(df -h / | tail -1 | awk '{print $5}')" >> /var/log/disk_trend.log

How to paste: Use Ctrl+Shift+V or right-click and paste. Go to the bottom of the file and add these lines.
วิธีวาง: ใช้ Ctrl+Shift+V หรือคลิกขวาแล้ววาง ไปที่ท้ายไฟล์และเพิ่มบรรทัดเหล่านี้

Save and exit / บันทึกและออก

Press Ctrl+X, then Y, then Enter

5. Manual Database VACUUM

5. VACUUM ฐานข้อมูลด้วยตนเอง

Install sqlite3 / ติดตั้ง sqlite3

sudo apt install sqlite3

Run VACUUM / รัน VACUUM

sqlite3 /var/lib/docker/volumes/n8n_data/_data/database.sqlite "VACUUM;"

6. n8n Execution Data Pruning

6. ลบข้อมูล Execution ของ n8n อัตโนมัติ

Find your docker-compose.yml file / หาไฟล์ docker-compose.yml ของคุณ

find /home -name "docker-compose.yml" 2>/dev/null

This shows the location of your docker-compose.yml file. Remember this path!
คำสั่งนี้แสดงตำแหน่ง ของไฟล์ docker-compose.yml จำ path นี้ไว้!

cd /path/to/your/n8n/folder

⚠️ Replace the path! Use the actual path from step 1. For example: cd /home/user/n8n
เปลี่ยน path! ใช้ path จริงจากขั้นตอน 1 เช่น: cd /home/user/n8n

Make a backup copy / สำรองไฟล์

cp docker-compose.yml docker-compose.yml.backup

Safety first! This creates a backup in case something goes wrong.
ความปลอดภัยก่อน! สร้างไฟล์สำรองกรณีมีปัญหา

Edit the docker-compose.yml file / แก้ไขไฟล์ docker-compose.yml

nano docker-compose.yml

Find the environment section / หาส่วน environment

Look for a section like this:
environment:
- N8N_HOST=...
- N8N_PORT=...

หาส่วนที่มีลักษณะแบบนี้:
environment:
- N8N_HOST=...
- N8N_PORT=...

Add these two lines / เพิ่มสองบรรทัดนี้

      - N8N_PRUNE_DATA=true
      - N8N_PRUNE_DATA_MAX_AGE=14

Add at the end of the environment section. Make sure the spacing matches other lines (6 spaces before the dash).
เพิ่มที่ท้าย ของส่วน environment ให้แน่ใจว่าการเว้นวรรคตรงกับบรรทัดอื่น (6 ช่องว่างก่อนเครื่องหมายขีด)

Save and exit / บันทึกและออก

Press Ctrl+X, then Y, then Enter

Restart n8n containers / รีสตาร์ท n8n containers

docker compose down && docker compose up -d

This stops and starts n8n with the new settings. It may take 1-2 minutes.
คำสั่งนี้หยุดและเริ่ม n8n ด้วยการตั้งค่าใหม่ อาจใช้เวลา 1-2 นาที

Verify it's working / ตรวจสอบว่าทำงาน

docker compose ps

Check the status - you should see "Up" status for n8n containers.
ตรวจสอบสถานะ - ควรเห็นสถานะ "Up" สำหรับ n8n containers


7. Send Daily Log Report to n8n Webhook

7. ส่งรายงานล็อกรายวันไปยัง n8n Webhook

Install jq / ติดตั้ง jq

sudo apt install -y jq

Create script / สร้างสคริปต์

sudo nano /root/send_logs_to_n8n.sh

Paste this code / วางโค้ดนี้

#!/bin/bash
# Replace YOUR_N8N_WEBHOOK_URL with your actual webhook URL
WEBHOOK_URL="https://your-n8n-instance.com/webhook/maintenance-logs"

DOCKER_LOG=$(tail -1 /var/log/docker_prune.log 2>/dev/null)
BINARY_LOG=$(tail -1 /var/log/binary_data_cleanup.log 2>/dev/null)
VACUUM_LOG=$(tail -1 /var/log/db_vacuum.log 2>/dev/null)
DISK_LOG=$(tail -1 /var/log/disk_trend.log 2>/dev/null)

curl -X POST -H "Content-Type: application/json" -d "$(jq -n \
  --arg docker "$DOCKER_LOG" \
  --arg binary "$BINARY_LOG" \
  --arg vacuum "$VACUUM_LOG" \
  --arg disk "$DISK_LOG" \
  '{docker_prune:$docker, binary_data_cleanup:$binary, db_vacuum:$vacuum, disk_trend:$disk}')" \
  "$WEBHOOK_URL"

⚠️ Important: Replace https://your-n8n-instance.com/webhook/maintenance-logs with your actual n8n webhook URL!
สำคัญ: เปลี่ยน https://your-n8n-instance.com/webhook/maintenance-logs เป็น URL webhook จริงของคุณ!

Save and exit / บันทึกและออก

Press Ctrl+X, then Y, then Enter

Make script executable / ทำให้สคริปต์รันได้

chmod +x /root/send_logs_to_n8n.sh

This step is required! Without this, the script won't run automatically.
ขั้นตอนนี้จำเป็น! ถ้าไม่ทำ สคริปต์จะไม่รันอัตโนมัติ

Add cron job / เพิ่ม cron job

sudo crontab -e

Add this line at the bottom / เพิ่มบรรทัดนี้ที่ท้ายไฟล์

15 6 * * * /root/send_logs_to_n8n.sh

This runs at 06:15 daily - 15 minutes after the disk logging, so all logs are ready.
จะรันเวลา 06:15 ทุกวัน - 15 นาทีหลังจากบันทึกดิสก์ เพื่อให้ log ทั้งหมดพร้อม

Save and exit / บันทึกและออก

Press Ctrl+X, then Y, then Enter

📊 Final Result

ผลลัพธ์สุดท้าย

  • 03:00 - Daily Docker cleanup / ล้าง Docker รายวัน
  • 04:00 - Daily binaryData cleanup / ล้าง binaryData รายวัน
  • 05:00 - Daily VACUUM of n8n database / VACUUM ฐานข้อมูล n8n รายวัน
  • 06:00 - Daily disk-usage logging / บันทึกการใช้ดิสก์รายวัน
  • 06:15 - Daily webhook report / ส่งรายงาน webhook รายวัน
  • Automatic - Execution data pruning by n8n / ลบข้อมูล execution อัตโนมัติโดย n8n

Quick Reference

Essential Commands:

# Check disk usage
df -h

# Find docker-compose.yml
find /home -name "docker-compose.yml" 2>/dev/null

# Manual Docker cleanup
docker system prune -af --volumes

# Database VACUUM
sqlite3 /var/lib/docker/volumes/n8n_data/_data/database.sqlite "VACUUM;"

# Restart n8n containers
docker compose down && docker compose up -d

# Check container status
docker compose ps

Cron Jobs Summary:

# Daily maintenance schedule
0 3 * * * docker system prune -af --volumes >> /var/log/docker_prune.log 2>&1
0 4 * * * find /var/lib/docker/volumes/n8n_data/_data/binaryData -type f -mtime +7 -delete >> /var/log/binary_data_cleanup.log 2>&1
0 5 * * * sqlite3 /var/lib/docker/volumes/n8n_data/_data/database.sqlite "VACUUM;" >> /var/log/db_vacuum.log 2>&1
0 6 * * * echo "$(date +'%F %T') $(df -h / | tail -1 | awk '{print $5}')" >> /var/log/disk_trend.log
15 6 * * * /root/send_logs_to_n8n.sh

Log Files Locations:

/var/log/docker_prune.log
/var/log/binary_data_cleanup.log
/var/log/db_vacuum.log
/var/log/disk_trend.log

Docker Compose Environment Variables:

# Add to your docker-compose.yml environment section
- N8N_PRUNE_DATA=true
- N8N_PRUNE_DATA_MAX_AGE=14

Need help? Contact us at ShantiLink.com 💬