Turkic settlement of the Tarim Basin
The Turkic peoples were descended from a Transeurasian agricultural community based in northeast China, and they were not recognized as native to the Xinjiang until the area was settled in by Tang-allied Türk (Tujue) tribes in the 7th century, and later by Turkic Uyghur people who founded the Qocho Kingdom there in the 9th century. The historical area of what is modern-day Xinjiang consisted of the distinct areas of the Tarim Basin (also known as Altishahr) and Dzungaria and was populated by Indo-European Tocharians and Saka peoples, who practiced Buddhism. They came under Chinese rule in the Han dynasty as the Protectorate of the Western Regions due to wars between the Han dynasty and the Xiongnu and again in the Tang dynasty as the Protectorate General to Pacify the West due to wars between the Tang dynasty and the First, Western, and Eastern Turkic Khaganates. The Tang dynasty withdrew its control of the region in the Protectorate General to Pacify the West and the Four Garrisons of Anxi after the An Lushan Rebellion, after which the Turkic peoples and the other native inhabitants living in the area gradually converted to Islam. Tarim Basin

The Tarim Basin, populated by the Indo-European Tocharian and Saka, became the place of settlement by two different Turkic Kingdoms, the Buddhist Turkic Uyghur Kingdom of Qocho and the Muslim Turkic Karluk Kara-Khanid Khanate. The Turfan and Tarim Basins were populated by speakers of Tocharian languages, with "Europoid" mummies found in the region. The oases were populated by Iranian and Tocharian language speakers. Different historians suggest that either the Sakas or Tokharians made up the Yuezhi people who lived in Xinjiang. The northern Tarim Basin is where Tokharian language records were found. The inhabitants of the Tarim Basin consisted of Buddhist Indo-Europeans, divided between Tocharians and Eastern Iranian Sakas. The discovery of the Tarim mummies has created a stir in the Turkic-speaking Uighur population of the region, who claim the area has always belonged to their culture, while it was not until the 10th century when the Uighurs are said by scholars to have moved to the region from Central Asia. American Sinologist Victor H. Mair claims that "the earliest mummies in the Tarim Basin were exclusively Caucasoid, or Europoid" with "east Asian migrants arriving in the eastern portions of the Tarim Basin around 3,000 years ago", while Mair also notes that it was not until 842 that the Uighur peoples settled in the area. Han and Tang rule

During the Han dynasty, the Tocharians and Sakas of Xinjiang came under a Chinese protectorate in 60 BC, with the Chinese protecting the Tocharian and Saka city states from the nomadic Xiongnu who were based in Mongolia, and during the Tang dynasty they once again became a protectorate of China with China protecting the Tocharian and Saka city states against the First Turkic Khaganate and the Turkic Uyghur Khaganate. Arab sources claim that first recorded incursion into the Tarim Basin by an Islamic force is the alleged attack on Kashgar by Qutayba ibn Muslim in 715 but some modern historians entirely dismiss this claim. The Tang dynasty Chinese defeated the Arab Umayyad invaders at the Battle of Aksu (717). The Arab Umayyad commander Al-Yashkuri and his army fled to Tashkent after they were defeated. Uyghur migration into the Tarim Basin

Tang China lost control of Xinjiang after it was forced to withdraw its garrisons during the An Lushan Rebellion. During the rebellion China received aid from the Uyghur Khaganate in crushing An Lushan's rebels, however, multiple provocations by the Uyghurs such as selling bad quality horses to China, practicing usury when lending to Chinese, and sheltering Uyghurs who committed murder resulted in a major deterioration in relations between China and the Uyghur Khaganate. Tang China then allied with the Yenisei Kirghiz and defeated and destroyed the Uyghur Khaganate in a war, triggering the collapse of the Uyghur Khaganate which caused Uyghurs to migrate from their original lands in Mongolia southwestwards into Xinjiang. Protected by the Taklamakan Desert from steppe nomads, elements of Tocharian culture survived until the 7th century, when the arrival of Turkic immigrants from the collapsing Uyghur Khaganate of modern-day Mongolia began to absorb the Tocharians to form the modern-day Uyghur ethnic group. Kara-Khanid conquest of Khotan

The Iranic Saka peoples originally inhabited Yarkand and Kashgar in ancient times, and were ruled by the Buddhist Saka Kingdom of Khotan and Zoroastrian Shule Kingdom when the first major Turkic incursions into the area began. The Saka Kings were still culturally-influenced by the Buddhist homeland of Northern India, with their rulers adopting Sanskrit names and titles. The Buddhist entities of Khotan and Dunhuang (in present-day Gansu) 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. The rulers of Khotan were aware of the menace they faced since they arranged for the Mogao grottoes to paint a growing number of divine figures along with themselves. By the time the Turkic Uyghur (Buddhist) and the Turkic Kara-Khanid (Muslim) invaded, Khotan was the only state in the area that had not come under Turkic rule. It was during the Karakhanid reign that Buddhism lost territory to Islam in the Kashgar area. A long war ensued between Islamic Kashgar and Buddhist Khotan which eventually ended in the conquest of Khotan by Kashgar. Many tales emerged about the Kara-Khanid ruling family's war against the Buddhists, and it is said that either Satok Bughra Khan's nephew or his grandson Ali Arslan was slain by the Buddhists during the war. A shrine dedicated to Ali Arslan or Sultan Arslan Boghra who was killed in the campaign against Khoton was the most popular shrine of the region. His body was buried on the field of battle at Ordam Padshah, to the east of Yangi Hissar, but his head is preserved at a shrine in the Dolat Bagh, near Kashgar. Satok Boghra Khan's last campaign was undertaken against Turfan, where in 993 he fell ill and whence he was carried back, a dying man, to Kashgar. His son and successor, Hasan, is known to history as having ended the Samanid dynasty by the capture of Abdul Malik. A few years later both Hasan and his brother were killed by the Princes of Khotan. In what proved to be a pivotal moment in the Islamicization of the Tarim Basin, the Karakhanid leader Yusuf Qadir Khan conquered Khotan around 1006. After the occupation by the Karakhanids, Turki became the universal language and the Saka language was banned while the records of it were to be destroyed. The oldest Kashgar book which has reached us, and which dates from 1068, is written in a pure Turki dialect. Taẕkirah Literature and the Legacy of the Khotan Conquest in Uyghur Sufism 
The Taẕkirah is a genre of literature written about Sufi Muslim saints in Altishahr. Written sometime in the period from 1700 to 1849, the Eastern Turkic language (modern Uyghur)  Taẕkirah of the Four Sacrificed Imams provides an account of the Muslim Kara-Khanid war against the Khotanese Buddhists. The Taẕkirah uses the story of the Four Imams as a device to frame the chronicle, the Four Imams being a group of Islamic scholars from Mada'in city (possibly in modern-day Iraq), who travelled to help the Islamic conquest of Khotan, Yarkand, and Kashgar by the Kara-Khanid leader Yusuf Qadir Khan. The legend of the conquest of Khotan is also given in the hagiology known as the Tazkirat or "Chronicles of Boghra". Extracts from the Tazkiratu'l-Bughra on the Muslim war against the  Khotan was translated by Robert Barkley Shaw. The shrines of Sufi Saints are revered in Altishahr as one of Islam's essential components and the tazkirah literature reinforced the sacredness of the shrines. It is even written in the Tazkirah of the Four Sacrificed Imams that "...those who doubt Their Holinesses the Imams will leave this world without faith, and on Judgement Day their faces will be black" . The fervor of the Qarakhand elite meant that in many cases, the ancient Chinese, Tocharian, Sogdian, Sanskrit, and Khotanese language texts dealing with Christianity, Manichaeanism, and Buddhism which contain important historical information were more or less ignored as sources of information for the region. After the Buddhist Khotanese killed Imam Shakir Padshah, a "Pigeons' Sanctuary" shrine arose in Khotan where pigeons "believed to be the offspring of a pair of doves which miraculously appeared from the heart of Imam Shakir Padshah" are taken care of and fed by pilgrims, this shrine was visited by Sir Aurel Stein. The conflict in which Xinijiang's Buddhist kingdoms were conquered are regarded as holy Jihad and the shehit (martyrs of Islam) who died in action in the wars are revered and worshiped at tombs called mazar by pilgrims in the present day. The Khotanese Buddhists killing of the martyr Imam Asim led to his grave being worshiped in a massive annual ceremony called the Imam Asim Khan festival. The writing and language of the Khotanese Indo European Eastern Iranics became extinct after the 11th century. Khotan was defeated and annexed by the war started by Kadir Khan Yusuf of the Karakhanids. Michael Dillon wrote that the 1000s-1100s Islam-Buddhist war are still recalled in the forms of the Khotan Imam Asim Sufi shrine celebration and other Sufi holy site celebrations. Bezeklik's Thousand Buddha Caves are an example of the religiously motivated vandalism against portraits of religious and human figures. Anti-portrait Muslims had Buddhist portraits obliterated during the wars over hundreds of years in which Buddhism was replaced by Islam. Cherrypicking of history of Xinjiang with the intention of projecting an image of irreligiousity or piousness of Islam or Buddhism in Uyghur culture has been done by people with agendas. Muslim works such as Ḥudūd al-ʿĀlam contained rhetoric and polemics against Buddhist Khotan, interpreted as "dehumanizing" the Khotanese Buddhists, and the Muslims Kara-Khanids conquered Khotan just 26 years following the completion of Ḥudūd al-ʿĀlam. Muslims gouged the eyes of Buddhist murals along Silk Road caves and Kashgari recorded in his Turkic dictionary an poem/folk song against Khotan. Sultan Satuq Bughra Khan and his son directed endeavors to proselytize Islam among the Turks and engage in military conquests. Buddhist temples and other non-Muslim buildings were razed by the Karakhanids once they converted to Islam in Kashgar. In 970 the Karakanids at first lost Kashgar to the Khotanese after a Khotanese military victory under Khotanese King Visa Sura who sent a "dancing elephant" to China along with a tool made of steel and a silver case with a cup, from the war booty seized in battle from the Karakhanids in addition to vessels, tools, leather armor, and jade as tribute, along with a letter in a royal edict to give the reason why the tribute was sent behind schedule and not on time to China, since the Khotanese just barely beat the Karakhanids out of Kashgar to its city limits and were holding it with great difficulty. From 970 the Karakhanids continued to fight the Khotanese until sometime before 1006 in the eventual conquest of Khotan by the Yusuf Qadir Khan, the Karakhanid leader. The Muslim attacks likely were the cause of requests for aid when China saw the arrival of multiple Khotanese diplomatic delegations. Defense assistance was requested of the Cao Guiyi rulers of Dunhuang after the Muslims were defeated by Visa Sura, Khotanese King in 970. After the victory over Kashgar Muslims at the hands of the Khotanese an elephant was sent as tribute by Khotan in 971 to Song dynasty China. After the Qara Khanid Turkic Muslims defeated the Khotanese under Yusuf Qadir Khan at or before 1006, China received a tribute mission in 1009 from the Muslims. The Encyclopaedia Sinica, by Samuel Couling says : "After Chang Ch'ien's mission, Khotan sent an embassy to China....... Till the end of the eighth century Chinese political influence remained potent, but then Tibet ended it, and Chinese annals are silent about Khotan for a century and a half. Then embassies were sent, probably seeking help against the Mohammedans, who, however, conquered the district at the end of the tenth century." The Buddhist Yutian (Khotan) experienced waves of jihads unleashed upon them by the Karakhanid leader Musa, the son of Satuk Bughra Khan although the jihads were unsuccessful, and the Khotanese managed to seize Kashgar after forcibly converted to Islam Buddhists revolted against the Karakhands in 969 the capital of the Karakhanids was temporarily seized by the Khotanese. In Yengisar in 998 the Yutian (Khotanese) managed to seize Kashgar and kill the Karakhanid leader Hasan in battle, striking the Karakhanids while the Karakhanids were busy fighting the Samanids. Buddhist Khotan was finally conquered when 140,000 mujahideen under Karakhanid leader Yusuf Kadir Khan attacked them. After 1006 when the Muslims won against the Khotanese they shut down Cave 17 in Dunhuang. 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 Guiyi circuit shut down the Khotanese libraries at Dunhuang after Khotan came under assault by Karakhanid Muslims. Cao Yuanzhong received a message from the King of Khotan in 970 that Khotan was under assault by Kashgari Muslim Karakhanids. The shutting down may have occurred after the Buddhists came under assault by the Muslims at Khotan. Buddhism was destroyed totally in the sanguine, decades long war in Khotan by the Karakhanids. Henri Cordier wrote in The book of Ser Marco Polo, the Venetian, concerning the Kingdoms and Marvels of the East" : "The Buddhist Government of Khotan was destroyed by Boghra Khán (about 980—990); it was temporarily restored by the Buddhist Kutchluk Khán, chief of the Na'i'mans, who came from the banks of the Ili, destroyed the Mahomedan dynasty of Boghra Khán (1209). but was in his turn subjugated by Chinghiz Khan. The only Christian monument discovered in Khotan is a bronze cross brought back by Grenard (iii. pp. 134—135); see also Gabriel Devéria, 'Notes d'Épigraphie Mongole,' p. 80.—H. C. "A Buddhist revival occurred in the Tangut Western Xia state since Muslims placed strain on the oasis states in Central Asia since before that most support for Buddhism came from Khotan. The Kara khanids were vanquishing the Khotan Buddhist state so the Western Xia Tangut state instead became the safe haven when Muslims attacked Indian Buddhist monks and drove them out of their lands. In 1006, the Muslim Kara-Khanid ruler Yusuf Kadir (Qadir) Khan of Kashgar conquered Khotan, ending Khotan's existence as an independent state. The Karakhanid Turkic writer Mahmud al-Kashgari recorded a short Turkic language poem about the conquest:

{|
! style="width:28em;"|
! style="width:28em;"|
! style="width:20em;"|
|-
|
English translation:

|
In Turkic (transcription by Robert Dankoff):

|
Modern Uyghur translation:

|}

Robert Dankoff has judged the poem to refer to Khotan's conquest by the Qarakhanids even though the text in Kashgari's work says it was against Uyghurs. Idols of "infidels" were subjected to desecration by being defecated upon by Muslims when the "infidel" country was conquered by the Muslims, according to Muslim tradition. It was written by Kashgari that - it is customary for the Muslims, when they captured a country of infidels, to defecate on the heads of their idols in order to profane them.. Besides the jihad against Yutian (Kingdom of Khotan), the Uygur Karakhoja Kingdom (Kingdom of Qocho) was also subjected to Jihad in which Buddhist temples were razed and Islam spread by the Karakhanid ruler Sultan Satuq Bughra Khan. The '"Compendium of the Turkic Dialects" by Kashgari, included among the infidels, the Uighurs. It was written "just as the thorn should be cut at its root, so the Uighur should be struck on the eye" by Kashgari, who viewed them as untrustworthy and noted that Muslim Turks used the derogatory name "Tat" against the Buddhist Uighurs whom Kashgari described as "infidels". The identities of "Buddhist" and "Uyghur" were intertwined with each other. The Buddhist Uighurs were subjected to an attack by the Muslim Turks and this was described in Mahmud Kashgari's work. Mahmud Kaşgari's works contained poetry stanzas and verses which described fighting between Buddhist Uighurs (Buddhist Turks) and Muslim Karakahnids (Muslim Turks). Uighur Buddhist temples were desecrated and Uighur cities were raided and Minglaq province across the river Ili was the target of the conquest against the Buddhist Uighur by the Muslim Karakhanids as described in 5-6 stanzas of Kashgari's work. Mahmud Kashgari insulted the Uyghur Buddhists as "Uighur dogs" and called them "Tats", according to the Tuxsi and Taghma, although other Turks called Persians "Tats". While Kashgari displayed a different attitude towards the Turks diviners beliefs and "national customs", he expressed towards Buddhism a hatred in his Diwan where he wrote the verse cycle on the war against Uighur Buddhists. Buddhist origin words like toyin (a cleric or priest) and Burxān or Furxan (meaning Buddha, acquiring the generic meaning of "idol" in the Turkic language of Kashgari) had negative connotations to Muslim Turks. Kashgari claimed that the Prophet assisted in a miraculous event where 700,000 Yabāqu were defeated by 40,000 Muslims led by Arslān Tegīn claiming that fires shot sparks from gates located on a green mountain towards the Yabāqu. The Yabaqu were a Turkic people. The non-Muslim Turks worship of Tengri was mocked and insulted by the Muslim Kashgari, who wrote about a verse referring to them - The Infidels - May God destroy them!The wars against the Buddhist, Shamanist, and Manichean Uighurs were considered a Jihad by the Kara-Khanids. The Uyghur state in eastern Xinjiang had tolerated Christianity and Buddhism but had opposed Islam. The Imams and soldiers who died in the battles against the Uyghur Buddhists and Khotan Buddhist Kingdom during the Tarim Basin's Islamization at the hands of the Karakhanids are revered as saints. It was possible the Muslims drove some Uighur Buddhist monks towards taking asylum in the Western Xia dynasty. Khizr Khoja's attack on Turfan and Qocho

In the 1390s, the Chagatai ruler Khizr Khwaja launched a holy war against the Kingdom of Qocho and Turfan, which were viewed as part of "Khitay" which was a name for China. Although Khizr Khwaja claimed to have converted to these kingdoms to Islam, the conversion was more gradual. Travellers passing through the area in 1420 remarked on the rich Buddhist temples, and only after 1450 were substantial numbers of mosques reported. As a consequence of the imposition of Islam, the city of Jiaohe was abandoned in the 15th century. Kara Del was a Mongolian ruled and Uighur populated Buddhist Kingdom. The Muslim Chagatai Khan Mansur invaded and used force to make the population convert to Islam. It was reported that between Khitay and Khotan the Sarigh Uyghur tribes who were "impious" resided, and they were targeted for ghazat (holy war) by Mansur Khan following 1516. After being converted to Islam, the descendants of the previously Buddhist Uyghurs in Turfan failed to retain memory of their ancestral legacy and falsely believed that the Dzungars were the ones who built Buddhist monuments in their area. Buddhist influences still remain among the Turfan Muslims. Since Islam reached them much after Altishahr, personal names of un-Islamic Old Uyghur origin are still used in Qumul and Turfan while people in Altishahr use mostly Islamic names of Persian and Arabic origin. The mujahideen of the Islamic Chagatai Khanate conquered the Uyghur and Hami was purged of the Buddhist religion which was replaced with Islam. The Islamic conversion forced on the Buddhist Hami state was the final event in the Islamization. Buddhist murals at the Bezeklik Thousand Buddha Caves were damaged by local Muslim population whose religion proscribed figurative images of sentient beings, the eyes and mouths in particular were often gouged out. Pieces of murals were also off for use as fertilizer by the locals. According to Joseph Fletcher, For centuries Altishar had been 'the abod of Islam'. Its inhabitants lived under the obligation of Jihad''. The 15th century was when Islamization finished off the Turkic Buddhists in Turfan. 