Skip to content

Feature Request: Automatic Message Cleanup for GOWS Engine #1657

@bergpinheiro

Description

@bergpinheiro

Feature Request: Automatic Message Cleanup for GOWS Engine

Summary

Add automatic cleanup of old messages from the gows_messages table in PostgreSQL for all GOWS sessions, configurable via environment variables with cron-based scheduling.

Problem

When using the GOWS engine with PostgreSQL storage, the gows_messages table can grow indefinitely, consuming disk space and potentially impacting database performance. Currently, there's no built-in mechanism to automatically clean up old messages.

Proposed Solution

Implement an automatic cleanup system that:

  1. Periodically deletes messages older than a configurable retention period
  2. Processes all GOWS sessions automatically
  3. Uses cron-based scheduling for flexible execution times
  4. Provides detailed logging of cleanup operations

Technical Implementation

New Files

  1. src/plus/engines/gows/services/GowsMessageCleanupService.ts

    • Service that handles message cleanup for individual sessions and all sessions
    • Accesses PostgreSQL databases using PsqlStore
    • Deletes messages where timestamp < cutoffDate
    • Returns count of deleted messages
  2. src/plus/engines/gows/services/GowsCleanupScheduler.ts

    • Scheduler service that uses cron-parser to schedule cleanup jobs
    • Implements OnModuleInit and OnApplicationShutdown
    • Calculates next execution time based on cron pattern
    • Prevents overlapping executions
  3. src/plus/engines/gows/gows.module.ts

    • NestJS module that registers the cleanup services
    • Validates environment variables using Joi
    • Conditionally loads when PostgreSQL is configured

Module Integration

  • Add GowsModule to src/plus/app.module.plus.ts with conditional loading when WHATSAPP_SESSIONS_POSTGRESQL_URL is defined
  • Export SessionManager and WhatsappConfigService from AppModulePlus for dependency injection

Environment Variables

Number of days to keep messages (default: 90)

WAHA_GOWS_MESSAGE_RETENTION_DAYS=30

Cron pattern for cleanup schedule (default: "0 0 2 * * *" = daily at 2:00 AM)

Format: second minute hour day month day-of-week

WAHA_GOWS_CLEANUP_CRON="0 0 2 * * *"### Cron Pattern Examples

  • "0 0 2 * * *" - Every day at 2:00 AM (default)
  • "0 30 3 * * *" - Every day at 3:30 AM
  • "0 0 */6 * * *" - Every 6 hours
  • "0 0 2 * * 0" - Every Sunday at 2:00 AM
  • "0 0 2 1 * *" - First day of every month at 2:00 AM

Features

✅ Automatic cleanup for all GOWS sessions
✅ Configurable retention period (days)
✅ Flexible scheduling via cron patterns
✅ Detailed logging of cleanup operations
✅ Error handling per session (doesn't stop if one session fails)
✅ Prevents overlapping executions
✅ Graceful shutdown support

Dependencies

  • cron-parser (already in project dependencies)

Logging

The implementation provides detailed logs:

  • Scheduler startup with configuration
  • Next scheduled execution time
  • Cleanup start/completion for each session
  • Number of messages deleted per session
  • Errors per session (non-blocking)

Example Log Output

patron:PRO

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions