Ukraine ordered the complete withdrawal from the decimated city of Avdiivka before dawn on Saturday, surrendering a position that had been a military stronghold for the better part of a decade, in the face of withering Russian assault.

“Based on the operational situation around Avdiivka, in order to avoid encirclement and preserve the lives and health of servicemen, I decided to withdraw our units from the city and move to defense on more favorable lines,” Gen. Oleksandr Syrsky, Ukraine’s top military commander, said in a statement issued overnight.

The fall of Avdiivka, a city that was once home to some 30,000 people but is now a smoking ruin, is the first major gain Russian forces have achieved since May of last year. After rebuffing a Ukrainian counteroffensive in the summer and fall, Russian forces in recent weeks have been pressing the attack across nearly the entire length of the 600-mile-long front.

The Ukrainian withdrawal on Saturday follows a bloody endgame that saw some of the fiercest fighting of the two-year-old war. Relying on its superiority in personnel and weaponry, Russia pounded the city with aerial bombardments and ground assaults, even as its fighters suffered a staggering amount of casualties.

Outgunned Ukrainian forces had begun withdrawing from positions in the southern part of the city on Wednesday, and since then have been engaged in a desperate battle to avoid encirclement inside the city as Russian forces advanced from multiple directions.

Oleksandr Tarnavskyi, the head of Ukraine’s forces in the south, said there had been no choice but to withdraw, given the Russian advantage in firepower and the number of soldiers Russia was willing to throw into the battle.