minimost.clean

minimost.clean

Maintenance utilities for purging old uploads and messages.

delete_files_older_than() removes old file attachments from uploads/. delete_messages_older_than() hard-deletes old rows from every per-user message database in users/. Both are called automatically by a background daemon thread started in minimost.create_app() — no cron job or external scheduler is required. The thread runs 5 minutes after startup and repeats every 24 hours. Retention periods are read from settings.json on each run:

  • "image_retention_days" — image file attachments (default: 30 days).

  • "file_retention_days" — all other file attachments (default: 30 days).

  • "message_retention_days" — messages in user databases (default: 770 days).

This module can also be invoked directly for ad-hoc cleanup:

python3 src/minimost/clean.py
minimost.clean.delete_files_older_than(directory: str, image_days: int, file_days: int, dry_run: bool = False)[source]

Delete files in directory based on type-specific retention periods.

Image files (jpg, jpeg, png, gif, webp) are removed when older than image_days; all other files are removed when older than file_days.

Parameters:
  • directory (str) – Path to the directory to clean.

  • image_days (int) – Retention period in days for image files.

  • file_days (int) – Retention period in days for non-image files.

  • dry_run (bool) – If True, only print what would be deleted without removing any files. Defaults to False.

Raises:

ValueError – If directory does not exist or is not a directory.

minimost.clean.delete_messages_older_than(users_dir: str, days: int, dry_run: bool = False)[source]

Hard-delete messages older than days from every user database.

Iterates every *.db file in users_dir and removes rows from the messages table whose ts timestamp predates the cutoff. Each database is processed independently so a single corrupted file does not abort the run.

Parameters:
  • users_dir (str) – Path to the directory containing per-user .db files.

  • days (int) – Messages older than this many days are deleted.

  • dry_run (bool) – If True, print what would be deleted without making any changes. Defaults to False.

Raises:

ValueError – If users_dir does not exist or is not a directory.

Usage

As a cron job (recommended — delete uploads older than 30 days at 02:30 every day):

30 2 * * * /usr/bin/python3 /srv/minimost/src/minimost/clean.py

From the command line:

python3 src/minimost/clean.py

Programmatically:

from minimost.clean import delete_files_older_than

# Preview what would be deleted (no files removed)
delete_files_older_than("uploads", days=30, dry_run=True)

# Delete files older than 14 days
delete_files_older_than("uploads", days=14)