Skip to content

Feature Request: Automatic Message Cleanup for GOWS Engine #1657

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

Activity

  1. WilliamSantos96 commented on Nov 24, 2025

    @WilliamSantos96

    up +

    patron:PLUS

  2. oasilap commented on Nov 24, 2025

    @oasilap

    +1

  3. StartecSistemas commented on Nov 24, 2025

    @StartecSistemas

    +1

    patron:PLUS

  4. mcabral2510 commented on Nov 24, 2025

    @mcabral2510

    +1

  5. snot1988 commented on Nov 24, 2025

    @snot1988

    +1

  6. devlikepro commented on Nov 25, 2025

    @devlikepro
    Contributor

    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

  7. bergpinheiro commented on Nov 25, 2025

    @bergpinheiro
    ContributorAuthor

    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

  8. theeusmartins commented on Dec 4, 2025

    @theeusmartins

    +1

    patron:PLUS

  9. favuka commented on Dec 9, 2025

    @favuka

    +1

  10. oasilap commented on Dec 9, 2025

    @oasilap

    +1

  11. Felipewesley69 commented on Dec 9, 2025

    @Felipewesley69

    +1

  12. vhiperdev commented on Dec 9, 2025

    @vhiperdev

    +1

    patron:PLUS

  13. jayveloper commented on Dec 9, 2025

    @jayveloper

    +1

    patron:PLUS

  14. 3 remaining items

  15. phmzum commented on Dec 18, 2025

    @phmzum

    +1

  16. jayveloper commented on Dec 24, 2025

    @jayveloper

    +1

    patron:PLUS

  17. jevil25 commented on Jan 2, 2026

    @jevil25

    +1

    patron:PLUS

  18. rafwell commented on Jan 28, 2026

    @rafwell

    +1

    patron:PRO

  19. higorrezend commented on Jan 29, 2026

    @higorrezend

    +1

    patron:PRO

  20. GoCode-io commented on Jan 29, 2026

    @GoCode-io

    +1

    patron:PRO

  21. Elimeshi1 commented on Jan 29, 2026

    @Elimeshi1
    Contributor

    +1

    patron:PLUS

  22. brunojfnet commented on Apr 3, 2026

    @brunojfnet

    +1

  23. trojantm commented on Apr 3, 2026

    @trojantm

    +1

    patron:PLUS

  24. brunocicom commented on Apr 15, 2026

    @brunocicom

    +1

  25. olavomachadoshibata commented on Apr 15, 2026

    @olavomachadoshibata

    +1

  26. simao-moreira commented on Apr 24, 2026

    @simao-moreira

    +1

  27. AnxHall commented on May 8, 2026

    @AnxHall

    +1

  28. personalrad commented on Aug 31, 2026

    @personalrad

    Hi. Is there any chance of this being implemented?

  29. WenderGalan commented on Oct 22, 2026

    @WenderGalan

    +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions