Mostafa Khan lived in an era of much political upheaval. It was the era of the Russo-Iranian Wars of the 19th century, a period during which the Russians took the Caucasus territories of Iran. In 1804, the Russians, led by general Pavel Tsitsianov, had invaded and sacked the Iranian town of Ganja, murdered its khan and his son, and had thereby initiated the Russo-Iranian War of 1804-1813. Having "shown" what Russia is capable of in terms of power and might, Tsitsianov thereafter attempted to force the other khans into submission to Russia by intimidation and enticement. Promising "Russian protection" and the guarantee that the khans would remain in power in their domains, the khan of Karabakh, Ibrahim Khalil Khan signed an agreement with Tsitsianov on 26 May 1805. 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. 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, 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 bring 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, 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 princeAbbas 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.