Why did most city states have defensive walls in ancient times?
There are two reasons for this. First, they had the walls because they were necessary. Second, they had the walls because they were effective.
Cities needed walls so that they could not be easily attacked. People from bandits to outright armies would have been interested in attacking and looting cities. The cities needed walls to make it so that attacking them would be too difficult. This would mean that they would be much less likely to be attacked.
Cities had walls because walls worked. In the times before modern artillery (cannons) walls were rather effective. Armies, let alone bandit bands, did not have effective ways to knock walls down from long distances. This meant that the walls were actually useful in ways that they were not useful after powerful cannon were invented.
Cities had walls, then, because they were effective ways to prevent attacks on those cities.