The Victory of Joshua over the Amorites (1625-1626), by Nicolas Poussin (1594-1665) [public domain / Wikimedia Commons]
This is how I have understood the matter, in my own apologetic reflection. Perhaps it will be helpful to others as well:
Because God is Creator He also has the prerogative to judge. This is analogous to our experience. Society takes it upon itself to judge the criminal and punish him if he supersedes the “just” laws that govern the society, in order to prevent chaos and suffering. If that is true of human society (one man to another), it is all the more of God, because He is ontologically above us (Creator and created). God directly commanded these wars.
So it is perfectly sensible and moral to posit (apart from the data of revelation) a notion of God judging both individuals and nations. God’s omniscience is such that He can determine if an entire nation has gone bad (“beyond repair,” so to speak) and should be punished. And He did so. Now, even in a wicked nation there may be individuals who are exceptions to the rule. So some innocent people will be killed. But this is like our human experience as well. In wartime, we go to war against an entire nation. In so doing, even if it is unintentional, some innocent non-combatants will be killed.
But it’s also different in God’s case because He judged nations in part in order to prevent their idolatry and other sins to infiltrate Jewish (i.e., true) religion. He also judged Israel at various times (lest He be accused of being unfair). In any event, it is not true that nations or individuals were punished because of what their ancestors did. There is a sense of corporate punishment, just described, and it is also true that the entire human race is a fallen race. We all deserve punishment for that fact alone, and God would be perfectly just to wipe us all out the next second. No one could hold it against Him.
He decides to be merciful and grant us grace to do better, but He is under no obligation to do so, anymore than the governor is obliged to pardon convicted criminals. Again, the societal analogy is perfectly apt. If someone rebels at every turn against every societal norm and law and appropriate behavior and so forth, is society to be blamed? Say someone grows up thinking that serial rape is fine and dandy and shouldn’t be prevented at all. So he goes and does this. Eventually, the legal system catches up with him and he gets his punishment. He rebelled against what most people think is wrong, and more than deserved his punishment.
We don’t say that there should be no punishment. We don’t blame society for his suffering in prison. We don’t deny that society has a right to judge such persons. So if mere human beings can judge each other, why cannot God judge His creation, and (particularly) those of His creation that have rebelled against Him at every turn? What is so incomprehensible about that? One may not believe it, but there is no radical incoherence or inconsistency or monstrous injustice or immorality in this Christian (and Jewish) viewpoint (which is what is always claimed by the critics).
[* = Internet Archive papers; allow a few minutes to upload]
Israel as God’s Agent of Judgment [9-28-14]
The Destruction of the Amalekites (RationalChristianity.net)
Genocide in the Old Testament (RationalChristianity.net)
God’s moral authority (RationalChristianity.net)
Does God Punish Children for Their Parents’ Sins? (RationalChristianity.net)
Does God Show favoritism to the descendants of good people? (RationalChristianity.net)
“What about God’s cruelty against the Midianites?” (Glenn Miller)