Yeah, pretty sure if you look at the context this is a retributive act because the Midianites had attacked the Israelites. I don't have time right now, but I'm fairly certain that attack was in violation of a treaty of safe passage. Also, the narrative of the Israelites entering the promised land is partially a judgment against the canaanites for practicing human and child sacrifice.
It's not easy, and it certainly doesn't make me feel happy, but it isn't a free-for-all license and command to kill everyone for no, or any, reason.