Under the Safavid dynasty of Iran, Shirvan was a leading silk manufacturer and its principal city, Shamakhi, became an important place for trade.
In 1734, the Iranian military leader Nader recovered Shirvan and installed Mohammad Mehdi Khan as its beglarbegi (governor-general).
[10] After the massacre in Ganja, Mostafa Khan asked the central government in Tehran for assistance, in order to prevent Tsitsianov's advance.
[11] Mostafa Khan hoped that the Russians would recognize a Shirvan Khanate "enlarged" to the boundaries of the Shirvanshah's of the Medieval era.
I declare before the entire world that I do not recognize anyone as my liege, except His Imperial Majesty, the Emperor of All the Russias, and His heirs to the throne.
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.
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.
[20] The war started off well for the Iranians; they quickly recaptured Ganja, Shirvan and Shaki amongst others, and performed attacks on Tiflis.
[20] However, just a few months later, the tide had completely turned with the Iranian army suffering decisive defeats against the militarily superior Russians.