Deuteronomy 20:10-18 ESV
10 “When you draw near to a city to fight against it, offer terms of peace to it. 11 And if it responds to you peaceably and it opens to you, then all the people who are found in it shall do forced labor for you and shall serve you. 12 But if it makes no peace with you, but makes war against you, then you shall besiege it. 13 And when the Lord your God gives it into your hand, you shall put all its males to the sword, 14 but the women and the little ones, the livestock, and everything else in the city, all its spoil, you shall take as plunder for yourselves. And you shall enjoy the spoil of your enemies, which the Lord your God has given you. 15 Thus you shall do to all the cities that are very far from you, which are not cities of the nations here. 16 But in the cities of these peoples that the Lord your God is giving you for an inheritance, you shall save alive nothing that breathes, 17 but you shall devote them to complete destruction, the Hittites and the Amorites, the Canaanites and the Perizzites, the Hivites and the Jebusites, as the Lord your God has commanded, 18 that they may not teach you to do according to all their abominable practices that they have done for their gods, and so you sin against the Lord your God.
Joshua 6:20-21 ESV
20 So the people shouted, and the trumpets were blown. As soon as the people heard the sound of the trumpet, the people shouted a great shout, and the wall fell down flat, so that the people went up into the city, every man straight before him, and they captured the city. 21 Then they devoted all in the city to destruction, both men and women, young and old, oxen, sheep, and donkeys, with the edge of the sword.
It's undeniable that a plain reading of these passages suggests that God commanded the complete annihilation not only of the adult inhabitants of Canaanite cities but also their children. Slaughtering children is universally recognized as a profound moral evil. If a group of soldiers were to invade our city, break into our homes, and kill and slaughter everyone, including babies and children, our fundamental moral instincts would undoubtedly recognize it as an egregious atrocity. In light of this, how can such actions be reconciled with the concept of a loving God?
If under any other circumstances, our moral compass would unequivocally condemn the slaughter of children as profoundly wrong—arguably the epitome of wrongdoing—why should the slaughter of Canaanite children be viewed as an exception? How could such a command be considered the most loving, just, and benevolent action that God could have taken?
To encourage objective answers, I'm primarily interested in exploring theodicies found in Christian literature (please provide references where applicable).
Note: My question has been prompted by recent discussions on YouTube, namely, William Lane Craig Defends the Canaanite Slaughter and "It's Horrific" | Reviewing WLC's Defense of the Slaughter of the Canaanites w/ Akin and Rauser.
A related passage from Psalm 137
8 O daughter of Babylon, doomed to be destroyed, blessed shall he be who repays you with what you have done to us! 9 Blessed shall he be who takes your little ones and dashes them against the rock!
I thank @NigelJ for the suggestion.