Can Langgraph postgresql_checkpointer framework implement a function which could delete the latest user ask and some ai messages but not delete the thread_id?

Can Langgraph postgresql_checkpointer framework implement a function which could delete the latest user ask and some ai messages but not delete the thread_id?

Hello @w123456789zy , welcome to Langchain community.

Yes, this is absolutely implementable:

The official LangGraph docs explicitly describe this as “Delete messages from LangGraph state permanently”:


from langchain_core.messages import RemoveMessage
from langgraph.graph.message import REMOVE_ALL_MESSAGES

# Get current state
state = graph.get_state(config)
messages = state.values["messages"]

# Find the last human message index
last_human_idx = next(
    i for i in reversed(range(len(messages)))
    if messages[i].type == "human"
)

# Build RemoveMessage list for last user ask + all following AI messages
msgs_to_remove = messages[last_human_idx:]  # human + all AI after it

# Apply via update_state — creates a new checkpoint, thread_id preserved
graph.update_state(
    config,
    {"messages": [RemoveMessage(id=m.id) for m in msgs_to_remove]},
)

  • Works with any checkpointer backend (Postgres, SQLite, Redis, MongoDB)
  • Thread stays alive: a new checkpoint is written on top, thread_id is never touched
  • The docs warn: make sure the resulting history is valid (some LLMs require history to start with a human message, or tool calls to be paired with tool results)

I hope this helps. Let me know if you have any more questions or confusion.