Cleaning Up After a Game

Learn what to do when game is over.

We'll cover the following

Cleaning up the tables

We’re saving the state of each game in the :game_state table, but so far, we haven’t removed that state when the game is over. The :game_state table continues to grow and use memory unless we delete a game’s key from the :game_state table when the supervisor terminates the child process or when the child process times out.

To clean that data up when a game ends normally, we add a call to :ets.delete/2 in the GameSupervisor.stop_game/1 function:

def stop_game(name) do
  :ets.delete(:game_state, name)
  Supervisor.terminate_child(__MODULE__, pid_from_name(name))
end

Get hands-on with 1400+ tech skills courses.