In 1828, Russia took full control of most of the area of present day Azerbaijan. Following the 1917 Bolshevik Revolution, Azerbaijan declared its independence. The first independent Azeri state, also the world's first secular Muslim state, lasted only three years, from 1918 until 1921. Russia's Red Army invaded the country and suppressed its independence effort. Azerbaijan was declared a part of Soviet Transcaucasia in 1922 and became a separate Union Republic in 1936. During the Stalin era, Azerbaijan suffered from the travails of collectivization and the purges.
With the disintegration of the Union of Soviet Socialist Republics, Azerbaijan was able to regain its independence in 1991. Ayaz Mutalibov was popularly elected to the post of president in an unopposed contest denounced as undemocratic by opposition parties. In 1992, Mutalibov was ousted by the Azerbaijani Popular Front (APF) and other opposition forces following Azerbaijani military losses in combating separatism in NK, a region inhabited by 180,000 ethnic Armenians.
The chairman of the Supreme Soviet, Yakub Mamedov, was named acting president and retained this position until he too was forced from power as a result of continuing military defeats in NK. Mutalibov tried to regain power but was forced to flee the country after APF-led crowds stormed the Supreme Soviet building and presidential palace. The APF assumed power and APF leader Abulfaz Elchibey was elected president in June 1992. His government was soon discredited by further military losses in NK, and an internal uprising brought Heydar (Geidar) Aliyev to power in June 1993.