Abellio ScotRail's contract to run train services in Scotland will end early, Transport Secretary Michael Matheson has announced.
The Dutch firm has been running the franchise since 2015 but had faced criticism over cancellations and performance levels.
The contract, worth more than £7bn over 10 years, was due to last until 2025.
But Mr Matheson said a so-called "break clause" would be used to end the franchise three years early, in 2022.
He told MSPs he was looking at other options for the future, including the franchise being run by the public sector.
What is the ScotRail franchise?
ScotRail operates about 2,400 train services a day, and covers all services in Scotland except those run by other operators which cross the border.
It is not responsible for the maintenance of the track, which falls under Network Rail nor the operation of the main rail routes from Glasgow and Edinburgh to London.
There had been political pressure on ministers over the service with complaints over a high number of cancellations, overcrowding and the cost of tickets.
In a statement to the Scottish Parliament, Mr Matheson said a proposed increase in government subsidy for the service would not deliver sufficient benefits to passengers or the economy.
Mr Matheson said Abellio had "successes and challenges" over the first five years of the ScotRail franchise, but that his decision was "the right one for passengers, communities, the economy and taxpayers".
He confirmed the contract would now run until 31 March, 2022.
"It is of course necessary to plan for the future provision of ScotRail services and I can confirm that work is already under way to examine the options open to the Scottish ministers after the current contract comes to an end," he said.
The franchise system for Britain's railways is grinding to a halt.
The Scottish government has jammed on the brakes and change is also expected soon at Westminster.
The reason given to MSPs for ending the franchise is 2022 stuck carefully to the contractual option on doing so and avoided the shortcomings of Abellio's handling of the franchise since 2015.
That would be open to legal challenge. Such a challenge would point out that the service has improved in the past year, with the arrival of new trains, adding more capacity. There's a new station in north-west Glasgow and improvements to the Aberdeen-Inverness service have just begun.
What happens next? Abellio is obliged to continue the service until the franchise ends.
The soon to be published Williams Review is expected to start a big shift away from the franchise bidding system. Pricing for tickets is expected to be shaken up.
In Scotland, there is pressure within the SNP, from unions, Labour and Greens, to put ScotRail operations in public hands. If there's to be another franchise, the government previously said it wants a public sector bidder for it. If there is to be one from April 2022, it will have to get up and running very soon.
What has been the reaction?
Dominic Booth, managing director of Abellio UK, said the firm was "hugely disappointed" and that the decision was "the wrong choice for Scotland's Railway and its customers".
He said the company had invested more than £475m in new and upgraded trains and created more than 500 extra jobs in Scotland.
Mr Booth added: "Our offer to Transport Scotland would have delivered an improved service for our customers at a reduced cost to the taxpayer."
But Kevin Lindsay, organiser for the train drivers' trade union Aslef in Scotland, claimed Abellio had let down passengers and staff.
He added: "The truth is that the franchise model is a broken business model. It hasn't worked, it doesn't work and it will not work. Everyone on the railway understands that."
Scottish Labour also welcomed the decision and called for the rail system to return to public ownership while the Scottish Liberal Democrats said the company had provided a "poor service".
Scottish Conservative shadow transport minister Jamie Greene described the termination of the contract as "an admission of absolute failure" by the SNP.