Well, in an anagogical sense, the OP is correct; those incidents are recorded to warn us of the last things, and of the judgement that we all must face.
And in an allegorical sense, they do represent spiritual warfare, and the triumph of Christ and his Church over Satan, sin, and false religions.
But there is a literal sense that cannot be ignored.
My own “solution” (feel free to dissect this, it’s just private reflection) is that God has to work with human limitations.
First: Even if God had wanted a “peaceful” solution or “negotiation” between the Israelites and, say, the Amalekites, it wouldn’t have worked, because of the wickedness of men, but also because of the culture at that time. Men of that age in history simply didn’t work that way. It has taken the direct revelation of Christ Himself for us to accept that peacemakers are, indeed, blessed, and for that idea to penetrate our culture. Divine Providence ordained the solution that would have preserved the maximum number of lives and souls, Israelite and non-Israelite. (Remember that many of the children being “butchered” may have been invincibly ignorant, and could have attained eternal life.)
Second: we must acknowledge, as Job did, that God knows best. The consequences of an act can reach across centuries and millennia. For a very trivial example, consider the following: was it unfair of God to cause Naomi’s husband and two sons to die, and leave her (and her foreign daughter-in-law) poor and dependent on charity? Not so, because the eventual consequence of this event was Ruth and Boaz’s marriage (immediate), the rise of David as the greatest of the Israelite kings (some centuries later) and finally the Davidic line that gave rise to St. Joseph, Our Blessed Mother, and Jesus Christ Himself (1200 years or so later). So though we may instinctly be revolted by the battles in the Old Testament, we must accept that God permitted these things to happen because they were part of His plan. To say otherwise would reduce the Israelites to the level of Assyrians or Babylonians, and would sever any meaningful connection between the OT and NT.
Third: being omniscient, God knows not just what will happen, but “what might have been” if a different course is taken. When He makes a choice, or commands the Israelites to take action in a particular way, He has also considered the alternatives. So while particular acts might seem horrific to our modern minds, the alternatives might have been much worse. (Compare the annals of other wars, battles or conquests, both in the past and in our “civilized” age, for some examples.)
I realise none of these are perfect solutions, and they could never be, because I can’t read the mind of God.

But even when we can only see “through a glass, darkly”, we have to trust that He knows what he is doing.