In the tenth century, the Buddhist Iranic Saka Kingdom of Khotan was the only city-state that was not conquered yet by the Turkic Uyghur (Buddhist) and the Turkic Qarakhanid (Muslim) states.
The Buddhist entitites of Dunhuang and Khotan had a tight-knit partnership, with intermarriage between Dunhuang and Khotan's rulers and Dunhuang's Mogao grottos and Buddhist temples being funded and sponsored by the Khotan royals, whose likenesses were drawn in the Mogao grottoes.
Halfway in the 10th century Khotan came under attack by the Qarakhanid ruler Musa,  a long war ensued between the Turkic Karakhanid and Buddhist Khotan which eventually ended in the conquest of Khotan by Kashgar by the Karakhanid leader Yusuf Qadir Khan around 1006.

Accounts of the Muslim Karakhanid war against the Khotanese Buddhists are given in  Taẕkirah of the Four Sacrificed Imams written sometime in the period from 1700-1849 which told the story of four imams from Mada'in city (possibly in modern-day Iraq) who travelled to help the Islamic conquest of Khotan, Yarkand, and Kashgar by Yusuf Qadir Khan, the Qarakhanid leader.
The "infidels" were defeated and driven towards Khotan by Yusuf Qadir Khan and the four Imams, but the Imams were assassinated by the Buddhists prior to the last Muslim victory.
 After Yusuf Qadir Khan's conquest of new land in Altishahr towards the east, he adopted the title "King of the East and China".

In 1006, the Muslim Kara-Khanid ruler Yusuf Kadir (Qadir) Khan of Kashgar conquered Khotan, ending Khotan's existence as an independent state.
The Islamic conquest of Khotan led to alarm in the east and Dunhuang's Cave 17, which contained Khotanese literary works, was closed shut possibly after its caretakers heard that Khotan's Buddhist buildings were razed by the Muslims, the Buddhist religion had suddenly ceased to exist in Khotan.
The Karakhanid Turkic Muslim writer Mahmud al-Kashgari recorded a short Turkic language poem about the conquest:


 
English translation:
We came down on them like a flood,

We went out among their cities,

We tore down the idol-temples,

We shat on the Buddha's head!

 
In Turkic:
kälginläyü aqtïmïz

kändlär üzä čïqtïmïz

furxan ävin yïqtïmïz

burxan üzä sïčtïmïz
In the tenth century, the Buddhist Iranic Saka Kingdom of Khotan was the only city-state that was not conquered yet by the Turkic Uyghur (Buddhist) and the Turkic Qarakhanid (Muslim) states. The Buddhist entitites of Dunhuang and Khotan had a tight-knit partnership, with intermarriage between Dunhuang and Khotan's rulers and Dunhuang's Mogao grottos and Buddhist temples being funded and sponsored by the Khotan royals, whose likenesses were drawn in the Mogao grottoes. Halfway in the 10th century Khotan came under attack by the Qarakhanid ruler Musa,  a long war ensued between the Turkic Karakhanid and Buddhist Khotan which eventually ended in the conquest of Khotan by Kashgar by the Karakhanid leader Yusuf Qadir Khan around 1006.
Accounts of the Muslim Karakhanid war against the Khotanese Buddhists are given in  Taẕkirah of the Four Sacrificed Imams written sometime in the period from 1700-1849 which told the story of four imams from Mada'in city (possibly in modern-day Iraq) who travelled to help the Islamic conquest of Khotan, Yarkand, and Kashgar by Yusuf Qadir Khan, the Qarakhanid leader. The "infidels" were defeated and driven towards Khotan by Yusuf Qadir Khan and the four Imams, but the Imams were assassinated by the Buddhists prior to the last Muslim victory.  After Yusuf Qadir Khan's conquest of new land in Altishahr towards the east, he adopted the title "King of the East and China".
In 1006, the Muslim Kara-Khanid ruler Yusuf Kadir (Qadir) Khan of Kashgar conquered Khotan, ending Khotan's existence as an independent state. The Islamic conquest of Khotan led to alarm in the east and Dunhuang's Cave 17, which contained Khotanese literary works, was closed shut possibly after its caretakers heard that Khotan's Buddhist buildings were razed by the Muslims, the Buddhist religion had suddenly ceased to exist in Khotan. The Karakhanid Turkic Muslim writer Mahmud al-Kashgari recorded a short Turkic language poem about the conquest:


 
English translation:
We came down on them like a flood,

We went out among their cities,

We tore down the idol-temples,

We shat on the Buddha's head!
 
In Turkic:
kälginläyü aqtïmïz

kändlär üzä čïqtïmïz

furxan ävin yïqtïmïz

burxan üzä sïčtïmïz
Tarim Basin