When Joshua was old and felt his time was near, he called to him representatives and elders from all the tribes to enjoin them to remember to always follow all God’s rules and be faithful subjects. He especially reminded them how important it was for them to never, ever, never worship other gods because that would incur God’s wrath and It would abandon them.
He reminded everyone that the Hebrews were God’s Chosen People, but that they would risk forfeiting that status if they didn’t complete the commanded genocide and finish exterminating all the Canaanites, Hivites, Jebusites, etc. that still lived in the Promised Land. Besides, leaving some alive would only act as a snare to the Hebrews and seduce them into cheating on God with other gods. And God was a jealous god that would smite and then forsake them.