Here’s my thoughts:
Destroying entire populations in warfare was a practice that occurred throughout most of the ancient world, and by no means was limited to Israel. The Midianites, the Amorites, the Canaanites and whomever else the Lord gave license to completely destroy themselves more than likely employed the same measures against their own enemies that they conquered in war.
The whole of the law is do unto others as you would have them do unto you, if the midianites (for instance) can wipe out an entire population of their own enemies in warfare, than it is only just that the same can be done to them. An eye for an eye.
Israel is also here fighting against them because they are idolaters who turned from worshipping the true God, and this is also a means of executing punishment on them. When Israel forsook the Lord’s commandment they themselves were treated like the midianites and the people they had driven out, and were put to the sword by foreign conquerers just as they had done to others.