Overview
By default, Mem0 memories persist forever. This works for user preferences and core facts, but temporary data should expire automatically. In this tutorial, we will:- Understand default (permanent) memory behavior
- Add expiration dates for temporary memories
- Decide what should be temporary vs permanent
Setup
Import
datetime and timedelta to calculate expiration dates. Without these imports, you’ll need to manually format ISO timestamps—error-prone and harder to read.Default Behavior: Everything Persists
By default, all memories persist forever:The Problem: Memory Bloat
Without expiration, memories accumulate forever. Session notes from weeks ago mix with current preferences. Storage grows, search results get polluted with irrelevant old context, and retrieval quality degrades.Memory bloat degrades search quality. When “User prefers dark mode” competes with “Currently browsing electronics” from 6 months ago, semantic search returns stale session data instead of actual preferences. Old memories pollute retrieval.
Short-Term Memories: Adding Expiration
Setexpiration_date to make memories temporary:
Expected behavior: After 7 days, the session context automatically disappears—no cron jobs, no manual cleanup. The preference persists forever. Mem0 handles expiration transparently.
expiration_date are automatically removed after expiring. No cleanup job needed - Mem0 handles it.
Start conservative with short expiration windows (7 days), then extend them based on usage patterns. It’s easier to increase retention than to clean up over-retained stale data. Monitor search quality to find the right balance.
When to Use Each
Permanent Memories (no expiration_date):
Use for:- User preferences and settings
- Account information
- Important facts and milestones
- Historical data that matters long-term
Temporary Memories (with expiration_date):
Use for:- Session context (current page, browsing history)
- Temporary reminders
- Recent chat history
- Cached data
Setting Different Expiration Periods
Different data needs different lifetimes:Using Metadata to Track Memory Types
Tag memories to make filtering easier:Checking Expiration Status
See which memories will expire and when:What You Built
A self-cleaning memory system with automatic retention policies:- Automatic expiration - Memories self-destruct after defined periods, no cron jobs needed
- Tiered retention - 7-day session context, 30-day chat history, permanent preferences
- Metadata tagging - Classify memories by type (session, preference, chat) for filtered retrieval
- Expiration tracking - Check which memories will expire and when using
get_all()
Summary
Memory expiration keeps storage clean and search results relevant. Useexpiration_date for temporary data (session context, recent chats), skip it for permanent facts (preferences, account info). Mem0 handles cleanup automatically—no background jobs required.
Start by identifying what’s temporary versus permanent, then set conservative expiration windows and adjust based on retrieval quality.