By 1800, the provinces of Courland and Livonia which comprise Latvian territory had been incorporated into Imperial Russia. In 1917 German occupying forces prevented the formation of a national government or army immediately after the Russian Revolution. After Germany’s defeat in the First World, Latvia declared independence from Russia on 18 November 1918. The country was soon invaded by the Soviet Red Army in January 1919, but a combined Latvian, Estonian and German force succeeded in liberating Latvia in early 1919. The Germans then made an unsuccessful attempt to take over, but were defeated by Latvian forces. Latvian independence was recognised by the USSR by the Treaty of Riga in 1920.
A new constitution was adopted in 1922, and a non-aggression treaty with the USSR ratified in 1932. In 1934 a nationalist dictatorship was established to restore political stability. During 1939 many citizens of German descent (Baltic Germans) were repatriated to Germany.
Under the 23 August 1939 Russian-German Non-Aggression Pact, the Baltic States were secretly assigned to the Soviet Union. Following the defeat of Poland by Germany in September 1939, Latvia was forced to accept a mutual assistance treaty with the USSR. On 17 June 1940 Latvia, Estonia and Lithuania were invaded by Soviet troops. Soviet-style elections were organised and the resulting communist parliament applied for membership of the Soviet Union. This was granted on 3(5?) August 1940 as the Latvian Soviet Socialist Republic.
On 22 June 1941 Germany invaded the Soviet Union. Latvia was soon occupied by units of the German Army, who were welcomed as liberators. In 1944 the Soviet Red Army retook the territory and re-imposed control. Many nationalist sympathisers were deported or killed.
During subsequent years immigration reduced the Latvian proportion of the population to only 52%. In the 1980s, this fact fostered increasing nationalist feeling, and eventually led to the formation of groups dedicated to establishing independence. When Lithuania and Estonia challenged Soviet authority, Latvia’s Supreme Soviet followed suit by declaring sovereignty on 28 July 1989. In February 1990, the Latvian parliament voted to work towards independence. On 4 May 1990 Latvia’s 1940 union with the USSR was declared illegal.
In January 1991, Soviet Interior Ministry troops violently seized some buildings in Riga. A subsequent referendum on independence was approved overwhelmingly by voters and on 21 August 1991, following the Soviet coup, the Latvian parliament declared full independence. The Soviet Union recognised Latvian independence on 6 September 1991. The last Russian troops were withdrawn by 31 August 1994. In November 2002 Latvia was invited to join NATO, and it formally joined on 2 April 2004.