World War I devastated the territory of what became the state of Latvia, and other western parts of the Russian Empire.
Demands for self-determination were initially confined to autonomy, until a power vacuum was created by the Russian Revolution in 1917, followed by the Treaty of Brest-Litovsk between Russia and Germany in March 1918, then the Allied armistice with Germany on 11 November 1918.
On 18 November 1918, in Riga, the People's Council of Latvia proclaimed the independence of the new country, with Kārlis Ulmanis becoming the head of the provisional government. The General representative of Germany August Winnig formally handed over political power to the Latvian Provisional Government on 26 November.
The war of independence that followed was part of a general chaotic period of civil and new border wars in Eastern Europe. By the spring of 1919, there were actually three governments: the Provisional government headed by Kārlis Ulmanis, supported by Tautas padome and the Inter-Allied Commission of Control; the Latvian Soviet government led by Pēteris Stučka, supported by the Red Army; and the Provisional government headed by Andrievs Niedra and supported by the Baltische Landeswehr and the German Freikorps unit Iron Division.
Estonian and Latvian forces defeated the Germans at the Battle of Wenden in June 1919, and a massive attack by a predominantly German force—the West Russian Volunteer Army—under Pavel Bermondt-Avalov was repelled in November. Eastern Latvia was cleared of Red Army forces by Latvian and Polish troops in early 1920 (from the Polish perspective the Battle of Daugavpils was a part of the Polish–Soviet War).