Shirvan Khanate

Shirvan Khanate (; ) was a khanate founded by the Afsharid dynasty that existed in what is now Azerbaijan in 1748—1820.
History.
In 1742, Shamakhi was taken and destroyed by Nader Shah of Persia, who relocated inhabitants into a new town under the same name about 16 miles to the west (now Agsu), at the foot of the main chain of the Caucasus. The "new" Shamakhi was a residence of the Khan of Shamakhi Hajji Muhammed Ali Khan, who ruled until 1765.
The smaller Old Shamakhi Khanate continued to exist under brothers Muhammad Said khan and (1748—1786) Aghasi Khan until the two killed Hajji Muhammed Ali Khan and united two khanates. After the merge, the New Shamakha was finally abandoned, and the old town rebuilt in 1786. During 1768—1789 the Shirvan khanate was occupied by the much stronger Quba Khanate and Shaki Khanate. Aghasi Khan was blinded and Muhammad Said Khan was taken captive to Derbent.
Children of Aghasi Khan and Muhammad Said khan were killed or exiled from Shirvan. Once Fatali Khan died, they came back restored their khanate. Askar Khan, Qasim Khan, and Mostafa Khan ruled after the decline of Quba Khanate. In 1795 the Russians captured Shamakhi as well as Baku as part of Persian Expedition of 1796 by Valerian Zubov. However, the campaign was abandoned and the army recalled after Catherine's death.
After the massacre in Ganja, Mostafa Khan asked the central government in Tehran for assistance, in order to prevent Tsitsianov's advance. The government responded by sending an army under general Pir-Qoli Khan Qajar. However, when the general had reached the Mughan plain, he found out that Mostafa Khan had entered negotiations with the Russians. Mostafa Khan hoped that the Russians would recognize a Shirvan Khanate "enlarged" to the boundaries of the Shirvanshah's of the Medieval era. Though Mostafa Khan was uncomfortable with Tsitsianov's proposal, the latter threatened that if he wouldn't agree with his terms, he would replace Mostafa with his younger brother (who was reportedly enthusiastic about it). Anyhow, the Russians invaded the khanate, and on 6 January 1806, Mostafa Khan was forced to submit.
Mostafa Khan was allowed to administer the khanate and had to give an annual tribute in gold rubles to the Russians. Furthermore, he had to send hostages to Tiflis (Tbilisi), which had recently been annexed and transformed into the "base" of the Russian Caucasus Viceroyalty. Lastly, he also had to provide food and accommodation for the Russian garrisons. After Tsitsianov was killed in Baku in 1806, Mostafa Khan repudiated his allegiance to the Russians, and re-submitted himself to the shah.
Things changed when Aleksey Yermolov took office as the new Russian commander-in-chief in the Caucasus, in 1816. A staunch Russian imperialist, Yermolov was committed to bringing the entire Caucasus under the Russian sway. He wanted to establish the Aras river as the border between Iran and Russia at all costs, and was therefore determined to conquer the last remaining khanates under Iranian rule; the Erivan Khanate and the Nakchivan Khanate.
When Ismail, the khan of Shaki, died in 1819 without any heir, Yermolov annexed the entity. Realizing what was going to happen to himself, Mostafa Khan fled to mainland Iran in 1820 with his son; Yermolov did not waste any time to annex the Shirvan Khanate.
Several years later, in violation of the Gulistan treaty (1813), the Russians invaded Iran's Erivan Khanate. This sparked the final bout of hostilities between the two; the Russo-Iranian War of 1826-1828. Crown prince Abbas Mirza led a full-scale attack in the summer of 1826 order to recover the Iranian territories that had been lost by the Gulistan treaty. The war started off well for the Iranians; they quickly recaptured Ganja, Shirvan and Shaki amongst others, and performed attacks on Tiflis. The government then reinstated Mostafa in Shirvan. However, just a few months later, the tide had completely turned with the Iranian army suffering decisive defeats against the militarily superior Russians. In September 1826, Abbas Mirza was defeated at Ganja by Ivan Paskevich, and thus the army had to retreat over the Aras. Mostafa Khan, accompanied by a small retinue, fled once again to mainland Iran.