Deuteronomy 31:18

And I will surely hide my face in that day for all the evils which they shall have wrought, in that they are turned unto other gods.

The hiding of the face belongs to a warning given before entry into the land, that after settling there and growing prosperous Israel would turn aside to idols. The face is hidden because they turn to other gods and break the covenant, thereby joining themselves to idolaters. That withdrawal is also because they do not take heed to the word spoken to them, do not receive reproof and correct their ways, trifle with their privileges, and set at naught counsel given them.

By withdrawing His presence and protection He leaves them exposed to destruction, evils, and troubles, so that in the midst of them they recognize that He is not among them. Being thus left diminishes their power and honor among the nations, and that abandonment issues in the desolation of Jerusalem with its walls in ruin and its gates broken down. The song Moses is commanded to teach is to remain in the mouths of their descendants and to stand as a testimony against them when those troubles come upon them.