Numbers 31:7

And they warred against the Midianites, as the LORD commanded Moses; and they slew all the males.

Israel warred against the Midianites in obedience to the Lord's command to Moses and slew all the males. The command charged Moses to avenge Israel before he was gathered to his people, and Israel carried it out exactly as commanded. The women and children were taken captive. Balaam was killed with the Midianites, along with Midian's kings.

The war was God's vengeance, judgment and punishment because the Midianites had led Israel into sin at Peor, seducing them into idolatry and transgression through Balaam's counsel. The Midianites had vexed Israel and sought its destruction, so they were treated as especially dangerous enemies. Israel acted as God's instruments of wrath and judgment, not to satisfy malice or revenge. The war was both justice against a guilty people and mercy toward Israel in granting victory over them.

The command and its execution were just and not cruel, because God as Maker and Ruler has full right to do as He pleases with the work of His hands. God bears long with nations that rebel against His authority, trample His law, scorn Him, and worship images of wood and stone and silver and gold made by their own hands, bringing swift destruction only when they have filled up the measure of their iniquity. God's chosen agents are to obey faithfully even when the work is repugnant to their natural feelings. The account teaches that God has authority over His creatures and that judgment for sin will come. The Midianites were nearly destroyed by Israel in Moses' time, but later grew numerous and powerful again and sought revenge.