Volkswagen said it had agreed to a deal with its union to reduce its workforce by more than 35,000 and cut billions of dollars a year in costs, while averting immediate factory closures in Germany.

Under the agreement announced Friday, VW said it would gradually reduce staffing levels “in a socially responsible way,” through early retirement and other measures. Workers will also forgo pay increases and lose certain bonuses. In exchange, the company agreed to not make any forced job cuts until 2030 and to keep all its German factories open. The cost-cutting plan comes as VW and other European carmakers contend with a tough economic environment, high domestic costs, abrupt shifts in demand for electric vehicles and increasing competition from lower-cost Chinese EV makers.

“The agreement is an important signal for the future viability of the Volkswagen brand,” Chief Executive Officer Oliver Blume said in a statement, adding that the company had set a “decisive course for its future in terms of costs, capacities and structures.” Friday’s deal concludes almost three months of negotiations between VW’s management and the company’s powerful union and works council. Friction between the two sides had led to some 100,000 workers temporarily walking off the job in recent weeks, with the union threatening wider strike action in the new year if an agreement wasn’t reached.