Sana'a is one of the oldest populated places in the world. According to popular legend, it was founded by Shem, the son of Noah. It was known as "Azal" in ancient times, which has been connected to Uzal, a son of Qahtan, a great-grandson of Shem, in the biblical accounts of Genesis. Its name is related to the Sabaic word for "well-fortified", a name that echoes the meaning of the Ethiopian name—recorded in a Syriac account as Auzalites—the city held in the 6th century.
The Arab historian al-Hamdani wrote that Sana'a was walled by the Sabeans under their ruler Sha'r Awtar, who also arguably built the Ghumdan Palace in the city. Because of its location, Sana'a has served as an urban center for the surrounding tribes of the region and as a nucleus of regional trade in southern Arabia. It was positioned at the crossroad of two major ancient trade routes linking Ma'rib in the east to the Red Sea in the west.
When King Yousef Athar (or Dhu Nuwas), the last of the Himyarite kings, was in power, Sana'a was also the capital of the Ethiopian viceroys.
From the dawn of Islam (ca. 622 CE) until the founding of independent sub-states in many parts of the Yemen Islamic Caliphate, Sana'a persisted as the governing seat. The Caliph's deputy ran the affairs of one of Yemen's three Makhalifs: Mikhlaf Sana'a, Mikhlaf al-Janad and Mikhlaf Hadhramaut. The city of Sana'a regularly regained an important status and all Yemenite States competed to control it.
Imam al-Shafi'i, the 8th-century Islamic jurist and founder of the Shafi'i school of jurisprudence, visited Sana'a several times. He praised the city, writing La budda min Ṣanʻāʼ, or "Sana'a must be seen." In the 9th–10th centuries, the Yemeni geographer al-Hamdani took note of the city's cleanliness, saying "The least dwelling there has a well or two, a garden and long cesspits separate from each other, empty of ordure, without smell or evil odors, because of the hard concrete (adobe and cob, probably) and fine pastureland and clean places to walk." Later in the 10th-century, the Persian geographer Ibn Rustah wrote of Sana'a "It is the city of Yemen — there cannot be found ... a city greater, more populous or more prosperous, of nobler origin or with more delicious food than it."
In 1062 Sana'a was taken over by the Sulayhid dynasty led by Ali al-Sulayhi and his wife, the popular Queen Asma. He made the city capital of his relatively small kingdom, which also included the Haraz Mountains. The Sulayhids were aligned with the Ismaili Muslim-leaning Fatimid Caliphate of Egypt, rather than the Baghdad-based Abbasid Caliphate which most of Arabia followed. Al-Sulayhi ruled for about 20 years but he was assassinated by his principal local rivals, the Zabid-based Najahids. Following his death, al-Sulayhi's daughter, Arwa al-Sulayhi, inherited the throne. She withdrew from Sana'a, transferring the Sulayhid capital to Jibla, where she ruled much of Yemen from 1067 to 1138. As a result of the Sulayhid departure, the Hamdanid dynasty took control of Sana'a.
In 1173 Saladin, the Ayyubid sultan of Egypt, sent his brother Turan-Shah on an expedition to conquer Yemen. The Ayyubids gained control of Sana'a in 1175 and united the various Yemeni tribal states, except for the northern mountains controlled by the Zaydi imams, into one entity. The Ayyubids switched the country's official religious allegiance to the Sunni Muslim Abbasids. During the reign of the Ayyubid emir Tughtekin ibn Ayyub, the city underwent significant improvements. These included the incorporation of the garden lands on the western bank of the Sa'ilah, known as Bustan al-Sultan, where the Ayyubids built one of their palaces. Despite Sana'a's strategic position, the Ayyubids chose Ta'izz as their capital while Aden was their principal income-producing city.
While the Rasulids controlled most of Yemen, followed by their successors the Tahirids, Sana'a largely remained in the political orbit of the Zaydi imams from 1323 to 1454 and outside the former two dynasties' rule. The Mamelukes arrived in Yemen in 1517.
The Ottoman Empire entered Yemen in 1538 when Suleiman the Magnificent was Sultan. Under the military leadership of Özdemir Pasha, the Ottomans conquered Sana'a in 1547. With Ottoman approval, European captains based in the Yemeni port towns of Aden and Mocha frequented Sana'a to maintain special privileges and capitulations for their trade. In 1602 the local Zaydi imams led by Imam al-Mu'ayyad reasserted their control over the area, and forced out Ottoman troops in 1629. Although the Ottomans fled during al-Mu'ayyad's reign, his predecessor al-Mansur al-Qasim had vastly weakened the Ottoman army in Sana'a and Yemen. Consequently, European traders were stripped of their previous privileges.
The Zaydi imams maintained their rule over Sana'a until the mid 19th-century, when the Ottomans relaunched their campaign to control the region. In 1835, Ottoman troops arrived on the Yemeni coast under the guise of Muhammad Ali of Egypt's troops. They did not capture Sana'a until 1872, when their troops led by Ahmed Muhtar Pasha entered the city. The Ottoman Empire instituted the Tanzimat reforms throughout the lands they governed.
In Sana'a, city planning was initiated for the first time, new roads were built, and schools and hospitals were established. The reforms were rushed by the Ottomans in order to solidify their control of Sana'a to compete with an expanding Egypt, British influence in Aden and imperial Italian and French influence along the coast of Somalia, particularly in the towns of Djibouti and Berbera. The modernization reforms in Sana'a were still very limited, however.
North Yemen period
In 1904, as Ottoman influence was waning in Yemen, Imam Yahya of the Zaydi imams took power in Sana'a. In a bid to secure North Yemen's independence, Yahya embarked on a policy of isolationism, avoiding international and Arab world politics, cracking down on embryonic liberal movements, not contributing to the development of infrastructure in Sana'a and elsewhere and closing down the Ottoman girls' school. As a consequence of Yahya's measures, Sana'a increasingly became a center of anti-government organization and intellectual revolt.
In the 1930s, several organizations opposing or demanding reform of the Zaydi imamate sprung up in the city, particularly Fatat al-Fulayhi, a group of various Yemeni Muslim scholars based in Sana'a's Fulayhi Madrasa, and Hait al-Nidal ("Committee of the Struggle.") By 1936 most of the leaders of these movements were imprisoned. In 1941 another group based in the city, the Shabab al-Amr bil-Maruf wal-Nahian al-Munkar, called for a nahda ("renaissance") in the country as well as the establishment of a parliament with Islam being the instrument of Yemeni revival. Yahya largely repressed the Shabab and most of its leaders were executed following his son, Imam Ahmad's inheritance of power in 1948. That year, Sana'a was replaced with Ta'izz as capital following Ahmad's new residence there. Most government offices followed suit. A few years later, most of the city's Jewish population emigrated to Israel.
Ahmad began a process of gradual economic and political liberalization, but by 1961 Sana'a was witnessing major demonstrations and riots demanding quicker reform and change. Pro-republican officers in the North Yemeni military sympathetic of Gamal Abdel Nasser of Egypt's government and pan-Arabist policies staged a coup overthrowing the Imamate government in September 1962, a week after Ahmad's death. Sana'a's role as capital was restored afterward.
 Neighboring Saudi Arabia opposed this development and actively supported North Yemen's rural tribes, pitting large parts of the country against the urban and largely pro-republican inhabitants of Sana'a. The North Yemen Civil War resulted in the destruction of some parts of the city's ancient heritage and continued until 1968 when a deal between the republicans and the royalists was reached, establishing a presidential system. Instability in Sana'a continued due to continuing coups and political assassinations until the situation in the country stabilized in the late 1970s.
British writer Jonathan Raban visited in the 1970s and described the city as fortress-like, its architecture and layout resembling a labyrinth", further noting "It was like stepping out into the middle of a vast pop-up picturebook. Away from the street, the whole city turned into a maze of another kind, a dense, jumbled alphabet of signs and symbols."
Attabari Elementary School, Old City of Sana'a
Following the unification of Yemen, Sana'a was designated capital of the new Republic of Yemen. It houses the presidential palace, the parliament, the supreme court and the country's government ministries. The largest source of employment is provided by the governmental civil service. Due to massive rural immigration, Sana'a has grown far outside its Old City, but this has placed a huge strain on the city's underdeveloped infrastructure and municipal services, particularly water.
Sana'a was chosen as the 2004 Arab Cultural Capital by the Arab League. In 2008, the Al Saleh Mosque was completed. It holds over 40,000 worshipers.
In 2011, Sana'a, as the Yemeni capital, was the center of the Yemeni Revolution in which President Ali Abdullah Saleh was ousted. Between May and November, the city was a battleground, in what became known as the 2011 Battle of Sana'a.
On 21 May 2012, Sana'a was attacked by a suicide bomber, resulting in the deaths of 120 soldiers.
On 23 January 2013, a drone strike near Al-Masna'ah village killed two civilians, according to a report issued by Radhya Al-Mutawakel and Abdulrasheed Al-Faqih and Open Societies Foundations.
On 21 September 2014, during the Houthi insurgency, the Houthis seized control of Sana'a.
On 12 June 2015, Saudi-led airstrikes targeting Shiite rebels and their allies in Yemen destroyed historic houses in the center of the capital. A UNESCO World Heritage site was severely damaged.
On 8 October 2016, Saudi-led airstrikes targeted a hall in Sana'a where a funeral was taking place. At least 140 people were killed and about 600 were wounded. After initially denying it was behind the attack, the Coalition's Joint Incidents Assessment Team admitted that it had bombed the hall but claimed that this attack had been a mistake caused by bad information.
On May 2017, according to the International Committee of the Red Cross an outbreak of cholera killed 115 people and left 8,500 ill. In late 2017, another Battle of Sana'a broke out between the Houthis and forces loyal to former President Saleh, who was killed.