structural:reformatting
Tang conquest and aftermath

In the 7th century, Emperor Taizong of Tang China, having overcome the Eastern Turks, sent his armies west to attack the Western Turks and the oasis states. The first oasis to fall was Turfan, which was captured in 630 and annexed as part of China.

Next to the west lay the city of Agni, which had been a tributary of the Tang since 632.Alarmed by the nearby Chinese armies, Agni stopped sending Tribute to China and formed an alliance with the Western Turks.They were aided by Kucha, who also stopped sending tribute.The Tang captured Agni in 644, defeating a Western Turk relief force, and made the king of Kucha Suvarnadeva (Chinese: 蘇伐疊 Sufadie) resume tribute.When that king was deposed by a relative named Haripushpa (Chinese: 訶黎布失畢 Helibushibi) in 648, the Tang sent an army under the Turk general Ashina She'er to install a compliant member of the local royal family, a younger brother of Haripushpa. Ashina She'er continued to capture Kucha, and made it the headquarters of the Tang Protectorate General to Pacify the West.Kuchean forces recaptured the city and killed protector-general, Guo Xiaoke, but it fell again to Ashina She'er, who had 11,000 of the inhabitants executed in reprisal for the killing of Guo. It was also recorded about other cities that "he destroyed five great towns and with them many myriads of men and women... the lands of the west were seized with terror." The Tocharian cities never recovered from the Tang conquest.

The Tang lost the Tarim basin to the Tibetan Empire in 670, but regained it in 692, and continued to rule there until it was recaptured by the Tibetans in 792. The ruling Bai family of Kucha are last mentioned in Chinese sources in 787. There is little mention of the region in Chinese sources for the 9th and 10th centuries.

The Uyghur Khaganate took control of the northern Tarim in 803.After their capital in Mongolia was sacked by the Yenisei Kyrgyz in 840, they established a new state, the Kingdom of Qocho with its capital at Gaochang (near Turfan) in 866. Over centuries of contact and intermarriage, the cultures and populations of the pastoralist rulers and their agriculturalist subjects blended together. Modern Uyghurs are the result of admixture between the Tocharians and the Orkhon Uyghurs from the 8th century CE. Many Uyghurs converted to the Tocharian Buddhism or Nestorian Christianity, and adopted the agricultural lifestyle and many of the customs of the oasis-dwellers. The Tocharian language gradually disappeared as the urban population switched to the Old Uyghur language.