One of the most difficult questions I've had in my faith is, "Why, in the Old Testament, did God sometimes command the killing of whole communities?"
People are frequently killed in natural disasters, or sometimes directly from the hand of God (as with the destruction of Sodom and Gomorrah) and sometimes people will ask why, but I don't think that God killing people or natural disasters killing people is as troublesome as the question above.
I think the reason this question is troublesome is that we don't want these instances to be used as a precedent. If God kills, well, he is all knowing and all wise and must have a good reason for what he is doing, but we have no such confidence in ourselves.
Fortunately, I think the Bible confirms that this is precisely the attitude we should take. The times in the Old Testament when God commanded killings were limited to very specific groups. There was never to my knowledge a general command to kill unbelievers, or Gentiles, or those of a different race. In fact, the command not to murder is right in the Ten Commandments, and before the Israelites entered the promised land, they were commanded to treat kindly the aliens in their midst (Deuteronomy 10:18-19, for example). The commands to kill were only against specific, named groups.
Further, that it was God who commanded these killings was testified to by decades of miraculous signs, and not penny-ante signs such as those performed by the Egyptian magicians. These were big miracles: the miracles of Moses in Egypt, the waters parting for the crossing of the Red Sea, manna from heaven, water from a rock, the pillars of cloud and fire, the Jordan River opening up to allow the Israelite army to pass over on dry ground, and the miraculous destruction of the walls of Jericho. The cumulative effect of these - it seems to me - was powerful testimony that God really did command these killings.
But we, on the other hand, have no list from God of groups of people to be killed. And even if we thought we did, we have no miraculous confirmations that such killings should be carried out. Far to the contrary. We have not only the Old Testament admonitions not to kill and to be kind to strangers, we have the strong testimony of the New Testament as well. And even if we saw miraculous signs, I don't think that would be adequate because the Bible makes clear that not all miracles are from God.
That is why the commands to kill whole groups of people should never be used as a precedent. But that still doesn't answer the question, "Why, in the Old Testament, did God sometimes command the killing of whole communities?"
Well, the reason is given in Genesis 15:16, before the exile in Egypt. Here God says that the people were evil, but that their sin had "not yet reached its full measure." Presumably, during the years Israel was in Egypt, their sin became worse, until it did "reach its full measure" and God was ready to destroy them.
Okay, but if the Canaanites were so evil, why didn't God just do away with them himself? Why did he drag the Israelites into it?
I don't know for sure, but my suspicion is that God wanted the Israelites to take sides. He wanted them to participate with him in the destruction of the Canaanites so there would be a deep gulf between monotheistic Israel and its polytheistic neighbors. He did not want fraternizing, so he cleared an area for Israel to live in and separated the people from their neighbors with fear and antagonism. (The Old Testament has lots of separating commands, such as not to eat various foods and even not to mix different kinds of fabrics. Through all these commands it seems God is pounding into the Israelites' heads the idea of separateness, perhaps so the seed of monotheism could gain root.) Further, I think God wanted the destruction of many of the cities, animals and people to be complete so nobody could say that the Israelites' success was built upon what Caanan and the Caananite "gods" had begun and also so that what remained would not be an enticement to the Israelites to drift from God. In Joshua 24:23, after the main part of the conquest, Joshua is already having to tell the Israelites to "throw away the foreign gods that are among you." I guess they had been picking up idols as souvenirs. But already? They're already playing around with foreign gods, after just seeing the power of God?
Finally, as I read the Old Testament and the story of the conquest, I am occasionally struck by instances of mercy. It is not as if God just let the Canaanites go to hell in a handbasket without any intervention on his part. I think of Balaam, through whom - despite his sin - God spoke. This suggests to me that God's voice had been heard in Canaan during the years while Israel was in captivity in Egypt. The people were not ignorant of God; God did reach out to them. The problem is that they rejected his overtures.
I also see this in God's miracles during the Exodus. In addition to proving himself to the people of Israel, these miracles served to warn the Canaanites. Rahab mentions that Canaan had heard what God had done for Israel, and was afraid (Joshua 2:9-11), which was God's intention (Joshua 4:23-24). But apparently - except, of course, for Rahab - that message did not lead many to repentance, though Rahab's acceptance into Israel shows that this door was open. But if it didn't lead many to repentence, perhaps it did lead many Canaanites to pack up their bags and move elsewhere.
I think also of the very harshness of the command to completely destroy the people, and I wonder if that wasn't in part designed to persuade the Canannites to leave, or to repent. But did the Canaanites know about the command? I'm certain they did. The Gibeonites, who tricked the Israelites (Joshua 9:24), knew about it, so it is reasonable to think that other communities knew as well. And was there really an opportunity for repentance? I think so. The situation reminds me of Jonah preaching to Nineveh that it would be destroyed, but when the people of Nineveh repented God did not destroy it. Perhaps if the Caananites had repented they would not have been destroyed either. Finally, Joshua 24:12 says God sent hornets ahead of the Israelites to drive the people away. So perhaps only the most stubborn of the Canaanites remained.