Course Content
LangChain Mastery
7 sections · 109 lessons
How do you clear memory in a LangChain conversation?
What you need to know
Three different "clears"
| Goal | How |
|---|---|
| Start a fresh chat, keep the old one | New thread_id (or session_id) |
| Delete one conversation completely | checkpointer.delete_thread(thread_id) |
| Remove some messages, keep the thread | RemoveMessage(id=...) in a state update |
| Forget a user entirely | Delete all their threads, store items, and logged copies |
LangGraph checkpointer
1from langchain.messages import RemoveMessage23# 1. Delete a whole thread (all checkpoints)4checkpointer.delete_thread("u-881:chat-17")56# 2. Remove specific messages but keep the thread7cfg = {"configurable": {"thread_id": "u-881:chat-17"}}8msgs = agent.get_state(cfg).values["messages"]9agent.update_state(cfg, {"messages": [RemoveMessage(id=m.id) for m in msgs[:10]]})delete_thread is part of every checkpointer (in-memory, SQLite, Postgres). RemoveMessage works because the messages key's reducer understands it: a RemoveMessage with a matching ID deletes that message. RemoveMessage(id=REMOVE_ALL_MESSAGES) (from langgraph.graph.message) clears all messages in the thread while keeping other state.
When removing messages, keep the history valid: do not leave an AI message with tool calls but remove its ToolMessage results, or the next model call will be rejected.
Legacy: chat message history
get_history("cust-42").clear() # SQL: deletes this session's rows; Redis: deletes the keyclear() is part of the BaseChatMessageHistory interface used with the deprecated RunnableWithMessageHistory, so the same line works for every backend. The legacy memory classes had memory.clear().
Long-term store
store.delete(("users", "u-881"), "preferences")for item in store.search(("users", "u-881"), limit=1000): store.delete(item.namespace, item.key)Designing it into the product
- "New chat" button — new thread ID; old one stays for the user's history list.
- Session expiry — a TTL where the checkpointer supports one (the Redis checkpointer does), or a nightly job that deletes threads older than your retention period (say 90 days).
- Right to erasure — India's DPDP Act and the EU's GDPR both give users a right to have personal data erased. Keep a list of thread IDs per user so you can find and delete everything, including copies in tracing tools and logs.
A real-life example
A food-delivery app's support bot kept threads forever in Postgres. A customer used the in-app "Delete my account" option, then complained that the bot, on a new account with the same phone number, greeted them with their old address.
The investigation found the account deletion job removed the user row but not the checkpointer threads or the long-term store items keyed by phone number. The team added a forget_user(user_id) function that calls delete_thread for every thread in the user's thread list, deletes their store namespace, and requests deletion of matching traces in LangSmith. They also set a 180-day retention job for inactive threads. A monthly audit samples 50 deleted users and confirms nothing remains.
Follow-up questions to expect
- "Is clearing memory the same as starting a new thread?" — No. A new thread leaves the old data stored;
delete_threadactually removes it. - "How do you clear memory in the legacy API?" —
memory.clear()on aConversationBufferMemory-style object, orhistory.clear()on a chat message history. - "What else holds conversation data?" — Traces, application logs, analytics events, vector stores of past turns and caches; erasure must cover them too.