How to delete a Phoenix Session?

Found it in the Plug Docs:

clear_session(conn)

Clears the entire session.

This function removes every key from the session, clearing the session.

Note that, even if clear_session/1 is used, the session is still sent to the client. If the session should be effectively dropped, configure_session/2 should be used with the :drop option set to true.


You can put something like this in your SessionsController:

def delete(conn, _) do
  conn
  |> clear_session()
  |> redirect(to: page_path(conn, :index))
end

and add a route for this in your web/router.ex.


I think what you're looking for is configure_session:

Plug.Conn.configure_session(conn, drop: true)

If you want to delete a particular session you should use:

conn |> fetch_session |> delete_session(:session_to_delete)

More informations here:

https://github.com/elixir-lang/plug/blob/master/lib/plug/session.ex#L114:L115