Sahih Bukhari 3021
It is mentioned in the Quran and God tells that it was down at the order or permission of God. It was a necessity to prepare space to invade the enemies.