In 1797, Karabakh suffered the invasion of armies of Persian shah Agha Mohammad Khan Qajar, who had just recently dealt with his Georgian subjects in the Tiflis. Shusha was besieged, but Agha Mohammad Khan Qajar was killed in the tent by own servants. In 1805 Ibrahim-khan signed the Kurekchay Treaty with Imperial Russia, represented by the Russian commander-in-chief in the war against Pavel Tsitsianov, according to which Karabakh khanate became the protectorate of Russia and the latter undertook to maintain Ibrahim-Khalil khan and his descendants as the ruling dynasty of Karabakh. However the following year Ibrahim-Khalil was killed by the Russian commandant of Shusha, who suspected that khan was trying to flee to Persia. Russia appointed Ibrahim-Khalil's son Mekhti-Gulu his successor.