How to handle memory cleanup/TTL with Simple Memory node?

My Setup:

  • AI Agent node with Simple Memory
  • Session key based on user’s WhatsApp ID: {{ $wa_id }}
  • Context window: 10 messages
  • Google Sheets for customer data storage

The Problem:

I want conversation memory to automatically expire after 2 hours of inactivity for each user. Currently, the memory persists indefinitely, which causes:

  1. Users returning after days still get old conversation context
  2. Memory bloat - old sessions accumulate in n8n’s database forever
  3. No built-in way to clear stale sessions

My Current Workaround:

I’m tracking last_order_date in Google Sheets and generating dynamic session IDs that change after 2 hours:

// If > 2 hours inactive
sessionId = `${wa_id}_${timeBucket}`;  // New session = old memory abandoned

// If < 2 hours inactive  
sessionId = wa_id;  // Same session = memory continues

This works but leaves orphaned sessions in the database.

My Questions:

  1. Is there a built-in way to set TTL (Time-To-Live) on memory sessions? I didn’t see this option in the Simple Memory node parameters.

  2. How does n8n handle memory cleanup internally? Do old sessions ever get deleted automatically, or do they persist indefinitely?

  3. What’s the recommended approach for session expiration?

    • Should I use an external Redis with TTL?
    • Is there a way to programmatically clear specific sessions?
    • Should I create a separate cleanup workflow?
  4. Does changing the session key abandon old memory, or does it get garbage collected? For example, if a user had session ID “123456” yesterday and “123456_new” today, does the old “123456” session data ever get deleted?

  5. Is this a common need? Should I open a feature request for TTL support in the memory node?

What I’ve Tried:

  • Using dynamic session IDs (current solution)
  • Searching documentation for TTL/expiration settings
  • Looking for cleanup workflows in the community

Environment:

  • n8n version: 1.118.2
  • Self-hosted on Ubuntu with Docker
  • ~500 users, ~300 messages/day

Any guidance would be greatly appreciated! Is there a best practice I’m missing, or is this something that should be a feature request?

Thanks! :folded_hands:

i think you should use Redis Chat Memory node. it has a TTL option

2 Likes

Thanks Ramin, I knew about redis but i thought it will charge me so i was thinking about free node. but after search i found out that it will not cost me money if i self hosted it.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.