In the book of Judges The Lord makes an interesting statement about the way that He disciplines His people. After the death of Joshua the Israelites began to worship other gods in the land of Canaan which displeased The Lord. The people knew nothing of their history as so were led a stray by the peoples that remained.
So, in much the same way that a mother lion will bring an injured animal to her young for them to kill, and so, learn something of the ways of nature, The Lord allowed some of the peoples in Canaan to remain and test the children of Israel.
As is written in Judges chapter 2:20-3:4
20 Therefore the Lord was very angry with Israel and said, “Because this nation has violated the covenant I ordained for their ancestors and has not listened to me, 21 I will no longer drive out before them any of the nations Joshua left when he died. 22 I will use them to test Israel and see whether they will keep the way of the Lord and walk in it as their ancestors did.” 23 The Lord had allowed those nations to remain; he did not drive them out at once by giving them into the hands of Joshua.
3 These are the nations the Lord left to test all those Israelites who had not experienced any of the wars in Canaan 2 (he did this only to teach warfare to the descendants of the Israelites who had not had previous battle experience): 3 the five rulers of the Philistines, all the Canaanites, the Sidonians, and the Hivites living in the Lebanon mountains from Mount Baal Hermon to Lebo Hamath. 4 They were left to test the Israelites to see whether they would obey the Lord’s commands, which he had given their ancestors through Moses.