To cleanse the land He had given, through divine promise, to Abraham’s offspring.
God knew, through His omniscient (all knowing) foreknowledge that Israel could not fully possess the land He had given them until the land had been cleansed of all peoples who were in rebellion against God. By worshipping in a pagan manner rather than in a manner pleasing to God, these people who had to be removed were unclean, and not fit for this special land in God’s holy eyes.
Israel sinned by not killing everyone who refused to leave the land of Promise voluntarily. God’s chosen people have paid a price in bloodshed and war ever since.