In the late Tang Dynasty, the Danghang tribe, which operated in present-day Shaanxi, Gansu and Ningxia, fought against the Song Dynasty in the early years of the Northern Song Dynasty under the leadership of its leader Li Jiquan, who captured Lingzhou in 1002, claimed the title of Xiping Prefecture, and moved the capital here. 1004, Li Jiquan died, and his son Deming assumed the throne, negotiated with the Song for peace, and shifted his goal to the Western Corridor of the Hexi River. 1028, he annihilated the migratory birds of Ganzhou, and 1030, the king of Guazhou surrendered with 1,000 riders. In the third year of Song Renzong's Jingyou (1036), the Western Xia captured Dunhuang and set up the Guazhou Xiping Supervisory Military Department to govern the area. Western Xia's local administrative organisation was also divided into state (prefectural, military), county (city, fortress) two-tier system, Dunhuang was still called Shazhou. At that time, Xixia was mainly fighting with Song and Liao in the east, and had no time to care about the west. Therefore, the control of Western Xia over Shazhou was still very weak at this time, and the local chiefs of Shazhou still maintained a certain degree of independence, and even the envoys of the Quaynai Army still paid tribute to the Song for seven times, which showed that the Quaynai Army still had a great deal of power in the long period of time when the Western Xia ruled Shazhou.
In 1038, Li Yuanhao became the emperor and established the Great Xia State, which was called Western Xia in history. After about 1052, the Western Xia strengthened its direct control over Guasha and Shazhou, and in 1062, in order to engage in a war with the Northern Song Dynasty, the Western Xia migrated the people of Dunhuang to the east, which greatly weakened Dunhuang, and in 1109, there was a famine in Guasha, Shazhou, and Sushu states, which led to an exodus of the people to other places. Since then, with the development of maritime transport and the decline of the land-based Silk Road, Dunhuang lost its position as a Silk Road trade transit point.
After the rise of the Mongols, they went on a campaign against the Western Xia, and in 1205, they invaded the Guasha region. 1224, the Mongol cavalry besieged Shazhou for half a year, and in March 1227, the Mongol Khanate occupied Dunhuang, abandoning the Shazhou establishment and assigning it to the fiefdom of Batu Daowang, which belonged to the fiefdom of Batu, the eldest grandson of Genghis Khan. In June of the same year, Mongolia destroyed Western Xia.
In 1271, shortly after the establishment of the Yuan Dynasty, Marco Polo passed through Shazhou. 1277, after the unification of China by the Yuan Dynasty, in order to strengthen the centralised power, the system of Xingzhongshu Province was implemented. The local administrative organisations were at provincial, road, prefectural, state and county levels, and the Yuan Dynasty re-established Shazhou under the jurisdiction of Suzhou, which was under the jurisdiction of the Yuan Dynasty's central government and granted local people the right to plant fields and agricultural tools. 1280, Shazhou was upgraded to a road, and a general administration was set up to govern the Guasha and Shazhou states, which were directly under the Gansu Province of the Central Committee of the Executive Yuan Dynasty. Yuan Shi - geography, "contained:" sand state road … … Yuan tai zu twenty-two years, broke its city, to subordinate eight all big king. To Yuan fourteen years to re-establish the state, seventeen places rose to Shazhou Road General Administration, Guazhou attached to it." Under the note said: "Shazhou to Suzhou thousands of 500 miles, the poor people attached to the inner, want to beg food Shazhou, must be white of Suzhou, and then given, the court of the court of the inconvenience, so the promotion of Shazhou for the road." Yuan Chengzong, for the development of Hexi, had been in Dunhuang and other places to implement the cantonment, Dade seven years (1303), the Imperial Palace of Ministers said: "Gua, sand two states, since the past for the border town of the important land, now the army cantonment Ganzhou, so that the officials and the people live outside the border, is not appropriate. Begging to Mongolia army ten thousand people divided town of dangerous passes, set up the military for the real, for the convenience." Dunhuang became an important base for the Yuan Dynasty to fight with the northwestern feudal lords. However, the major westward transport routes of the Yuan Dynasty no longer passed through Dunhuang, and Dunhuang lost its former geographical advantage. 1291-1292, the Yuan Dynasty moved the residents of Guashazhou into Suju, and Dunhuang's position became increasingly weakened.
The Ming Dynasty was established in 1368, and in 1372, the Ming general Feng Sheng made an initial victory in the western part of the river, but Dunhuang was still under the control of the remnants of the Yuan Dynasty. In order to prevent the Mongols from advancing eastward, Feng Sheng built Jiayuguan Pass seventy miles west of Suzhou, which became the western border gate of the Ming Dynasty, and Dunhuang was abandoned.1391, the Ming Dynasty sent troops to Hami, and the Mongolian princes in Shazhou submitted to the Ming Dynasty.1404, the Ming Dynasty set up Shazhou Guard, which was still under the control of the Mongols.1446, Shazhou was in civil unrest, and the Ming general Renli led the army into Shazhou, and moved more than two hundred households belonging to the Guard to Guannai, and Shazhou was still occupied by Mongols. Shazhou was still occupied by Mongol descendants. Later, Turpan became powerful and attacked the Mongols in Shazhou in 1515, and occupied Shazhou in 1528; in 1524, the Ming closed the Jiayuguan Pass and abandoned Hami in 1529. Dunhuang had already become a pastoral area in the early Ming Dynasty, and even more so by then.
In 1644, the Ming dynasty died and the Qing dynasty entered the country and set its capital at Beijing. In 1723, Emperor Yongzheng set up Shazhou in Dunhuang, which was upgraded to a guard in 1725, and moved more than 2,400 families here to canton. The old city of Shazhou was destroyed by water, and a new city was built in the east at this time. In the twentieth year of the Qianlong era (1760), Shazhouwei was changed to Dunhuang County, under the Ansu Road. Dunhuang was slightly restored during the Qing Dynasty through emigration, cantonment and reclamation, but without much improvement.