RemoveMessage is a special message type in LangGraph that instructs the add_messages reducer to delete a specific message from the conversation state by its id. It is used to surgically remove unwanted or outdated messages without resetting the entire history.
In LangGraph, the add_messages reducer normally appends or replaces messages. To delete a message, you can return a RemoveMessage object containing the id of the message to remove. The reducer will filter out that message from the state. This is useful for correcting mistakes, removing sensitive information, or implementing forgetfulness. RemoveMessage is imported from langgraph.graph.message.
You can also return a list of RemoveMessage objects to delete multiple messages at once. The deletion is applied when the reducer processes the update.