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.
[4] After the massacre in Ganja, Mostafa Khan asked the central government in Tehran for assistance, in order to prevent Tsitsianov's advance.
[5] Mostafa Khan hoped that the Russians would recognize a Shirvan Khanate "enlarged" to the boundaries of the Shirvanshahs of the Medieval era.
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.
[13] However, just a few months later, the tide had completely turned with the Iranian army suffering decisive defeats against the militarily superior Russians.