During and before the occupation of Derbent by Peter I, the naib of city was Imam Quli Khan and was naturally a Shiite like the rest of the Safavid Empire.
Peter I reappointed Imam Quli Khan as the head of Derbent and its "native" troops by assigning him the rank of Major-General.
In September 1723 following the Russo-Persian War (1722-1723) and the outcoming Treaty of Saint Petersburg, the Safavid Shah Sultan Husayn, whose empire was for years already in disarray and crumbling, was forced to cede Derbent alongside the many other Iranian territories in the Caucasus.
[6] However, some years later in connection with the aggravation of Russian-Turkish relations, and the new rise of Persia now led by the brilliant military general Nader Shah, Russia found itself forced to cede all territories back by March 1735 in the Treaty of Ganja in order to deter itself from a costly war against Persia, and also to construct an alliance against the common neighbouring foe; Ottoman Turkey.
In addition to the Derbent khanate according to the terms of the infamous treaty, it also was forced to irrevocably cede Baku, Karabakh, Ganja, Shirvan, Shaki, Cuban, Georgia, and wider Dagestan.