[citation needed]
Historically, at least initially, their primary purpose was to allow tribes to organize themselves in a way that would support labor-intensive farming practices. Large-scale early grain farming was not possible without an organized state.
As technological complexity grew, states grew as well. I’d argue that the closest thing to a primary “purpose” for states (not that there is one, states evolved organically) is to maintain the organization of industrial supply chains that perpetuate modern life. Defense is secondary; still important, but this comes after ensuring that resources are available to sustain life.
States are currently the primary perpetuators of violence, and they are presently faltering at their job of maintaining global resource flows, thanks to all the violence. If they fail completely, we all lose.