waha icon indicating copy to clipboard operation
waha copied to clipboard

Feature Request: Automatic Message Cleanup for GOWS Engine

Open bergpinheiro opened this issue 1 year ago • 30 comments

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

bergpinheiro avatar Nov 24 '25 18:11 bergpinheiro

up +

patron:PLUS

WilliamSantos96 avatar Nov 24 '25 18:11 WilliamSantos96

+1

oasilap avatar Nov 24 '25 18:11 oasilap

+1

patron:PLUS

StartecSistemas avatar Nov 24 '25 19:11 StartecSistemas

+1

mcabral2510 avatar Nov 24 '25 19:11 mcabral2510

+1

snot1988 avatar Nov 24 '25 19:11 snot1988

The endpoint to clean any tables would also help as the first thing

POST /api/{session}/store/clean/messages
{
  keep: 30 days
}

and we could add a Cleanup App so you could configure it globally or per session on crontab schedule 🤔

patron:PRO

devlikepro avatar Nov 25 '25 09:11 devlikepro

The endpoint to clean any tables would also help as the first thing

POST /api/{session}/store/clean/messages { keep: 30 days } and we could add a Cleanup App so you could configure it globally or per session on crontab schedule 🤔

patron:PRO

Yes, an endpoint is also a great option. Globally, it would also be a way to automate.

patron:PRO

bergpinheiro avatar Nov 25 '25 20:11 bergpinheiro

+1

patron:PLUS

theeusmartins avatar Dec 04 '25 19:12 theeusmartins

+1

favuka avatar Dec 09 '25 14:12 favuka

+1

oasilap avatar Dec 09 '25 14:12 oasilap

+1

Felipewesley69 avatar Dec 09 '25 14:12 Felipewesley69

+1

patron:PLUS

vhiperdev avatar Dec 09 '25 15:12 vhiperdev

+1

patron:PLUS

jayveloper avatar Dec 09 '25 16:12 jayveloper

+1

patron:PLUS

OrionZap avatar Dec 09 '25 16:12 OrionZap

+1

DiegoAugustusCunha avatar Dec 18 '25 13:12 DiegoAugustusCunha

+1

patron:PLUS

cadao7 avatar Dec 18 '25 13:12 cadao7

+1

phmzum avatar Dec 18 '25 13:12 phmzum

+1

patron:PLUS

jayveloper avatar Dec 24 '25 13:12 jayveloper

+1

patron:PLUS

jevil25 avatar Jan 02 '26 11:01 jevil25

+1

patron:PRO

rafwell avatar Jan 28 '26 15:01 rafwell