According to popular tradition documented by J. Gordon Melton, the first Shiva temple at Somnath is believed to have been built at some unknown time in the past.
The second temple is said to have been built at the same site by the "Yadava kings" of Vallabhi around 649 CE.
In 725 CE, Al-Junayd, the Arab governor of Sindh is said to have destroyed the second temple as part of his invasions of Gujarat and Rajasthan.
The Pratihara Gurjar king Nagabhata II is said to have constructed the third temple in 815 CE, a large structure of red sandstone.

However, there is no historical record of an attack on Somnath by Al-Junayd.
Nagabhata II is known to have visited tirthas in Saurashtra, including Someshvara (the Lord of the Moon), which may or may not be a reference to a Shiva temple because the town itself was known by that name.
The Chaulukya (Solanki) king Mularaja possibly built the first temple at the site sometime before 997 CE, even though some historians believe that he may have renovated a smaller earlier temple.

In 1024, during the reign of Bhima I, the prominent Turkic Muslim ruler Mahmud of Ghazni raided Gujarat, plundering the Somnath temple and breaking its jyotirlinga.
He took away a booty of 20 million dinars.
Historians expect the damage to the temple by Mahmud to have been minimal because there are records of pilgrimages to the temple in 1038, which make no mention of any damage to the temple.
However, powerful legends with intricate detail developed in the Turko-Persian literature regarding Mahmud's raid, which "electrified" the Muslim world according to scholar Meenakshi Jain.
They later boasted that Mahmud had killed 50,000 devotees who tried to defend the temple, a formulaic figure.

The temple at the time of Mahmud's attack appears to have been a wooden structure, which is said to have decayed in time (kalajirnam).
 Kumarapala (r. 1143–72) rebuilt it in "excellent stone and studded it with jewels," according to an inscription in 1169.

During its 1299 invasion of Gujarat, Alauddin Khalji's army, led by Ulugh Khan, defeated the Vaghela king Karna, and sacked the Somnath temple.
Legends in the later texts Kanhadade Prabandha (15th century) and Khyat (17th century) state that the Jalore ruler Kanhadadeva later recovered the Somnath idol and freed the Hindu prisoners, after an attack on the Delhi army near Jalore.
However, other sources state that the idol was taken to Delhi, where it was thrown to be trampled under the feet of Muslims.
These sources include the contemporary and near-contemporary texts including Amir Khusrau's Khazainul-Futuh, Ziauddin Barani's Tarikh-i-Firuz Shahi and Jinaprabha Suri's Vividha-tirtha-kalpa.
It is possible that the story of Kanhadadeva's rescue of the Somnath idol is a fabrication by the later writers.
Alternatively, it is possible that the Khalji army was taking multiple idols to Delhi, and Kanhadadeva's army retrieved one of them.

The temple was rebuilt by Mahipala I, the Chudasama  king of Saurashtra in 1308 and the lingam was installed by his son Khengara sometime between 1331 and 1351.
As late as the 14th century, Gujarati Muslim pilgrims were noted by Amir Khusrow to stop at that temple to pay their respects before departing for the Hajj pilgrimage.

In 1395, the temple was destroyed for the third time by Zafar Khan, the last governor of Gujarat under the Delhi Sultanate and later founder of Gujarat Sultanate.
In 1451, it was desecrated by Mahmud Begada, the Sultan of Gujarat.

By 1665, the temple, one of many, was ordered to be destroyed by Mughal emperor Aurangzeb.
In 1702, he ordered that if Hindus revived worship there, it should be demolished completely.
According to popular tradition documented by J. Gordon Melton, the first Shiva temple at Somnath is believed to have been built at some unknown time in the past. The second temple is said to have been built at the same site by the "Yadava kings" of Vallabhi around 649 CE. In 725 CE, Al-Junayd, the Arab governor of Sindh is said to have destroyed the second temple as part of his invasions of Gujarat and Rajasthan. The Pratihara Gurjar king Nagabhata II is said to have constructed the third temple in 815 CE, a large structure of red sandstone.
However, there is no historical record of an attack on Somnath by Al-Junayd. Nagabhata II is known to have visited tirthas in Saurashtra, including Someshvara (the Lord of the Moon), which may or may not be a reference to a Shiva temple because the town itself was known by that name. The Chaulukya (Solanki) king Mularaja possibly built the first temple at the site sometime before 997 CE, even though some historians believe that he may have renovated a smaller earlier temple.
In 1024, during the reign of Bhima I, the prominent Turkic Muslim ruler Mahmud of Ghazni raided Gujarat, plundering the Somnath temple and breaking its jyotirlinga. He took away a booty of 20 million dinars. Historians expect the damage to the temple by Mahmud to have been minimal because there are records of pilgrimages to the temple in 1038, which make no mention of any damage to the temple. However, powerful legends with intricate detail developed in the Turko-Persian literature regarding Mahmud's raid, which "electrified" the Muslim world according to scholar Meenakshi Jain. They later boasted that Mahmud had killed 50,000 devotees who tried to defend the temple, a formulaic figure.
The temple at the time of Mahmud's attack appears to have been a wooden structure, which is said to have decayed in time (kalajirnam).  Kumarapala (r. 1143–72) rebuilt it in "excellent stone and studded it with jewels," according to an inscription in 1169.
During its 1299 invasion of Gujarat, Alauddin Khalji's army, led by Ulugh Khan, defeated the Vaghela king Karna, and sacked the Somnath temple. Legends in the later texts Kanhadade Prabandha (15th century) and Khyat (17th century) state that the Jalore ruler Kanhadadeva later recovered the Somnath idol and freed the Hindu prisoners, after an attack on the Delhi army near Jalore. However, other sources state that the idol was taken to Delhi, where it was thrown to be trampled under the feet of Muslims. These sources include the contemporary and near-contemporary texts including Amir Khusrau's Khazainul-Futuh, Ziauddin Barani's Tarikh-i-Firuz Shahi and Jinaprabha Suri's Vividha-tirtha-kalpa. It is possible that the story of Kanhadadeva's rescue of the Somnath idol is a fabrication by the later writers. Alternatively, it is possible that the Khalji army was taking multiple idols to Delhi, and Kanhadadeva's army retrieved one of them.
The temple was rebuilt by Mahipala I, the Chudasama  king of Saurashtra in 1308 and the lingam was installed by his son Khengara sometime between 1331 and 1351. As late as the 14th century, Gujarati Muslim pilgrims were noted by Amir Khusrow to stop at that temple to pay their respects before departing for the Hajj pilgrimage.
In 1395, the temple was destroyed for the third time by Zafar Khan, the last governor of Gujarat under the Delhi Sultanate and later founder of Gujarat Sultanate. In 1451, it was desecrated by Mahmud Begada, the Sultan of Gujarat.
By 1665, the temple, one of many, was ordered to be destroyed by Mughal emperor Aurangzeb. In 1702, he ordered that if Hindus revived worship there, it should be demolished completely.
Somnath temple