A cheesemaker blamed for an outbreak of E.coli poisoning that led to the death of a three-year-old girl has been cleared of breaching food safety laws.
Errington Cheese had been accused of failing to comply with legal standards of food hygiene.
But in a 254-page ruling Sheriff Robert Weir QC said the allegations were "not well founded".
The 2016 outbreak involved Dunsyre Blue but that did not form part of the case, which focused on sheep's milk cheeses.
Stocks of Dunsyre Blue - which is made with cow's milk and was not considered during the Sheriff's ruling - were seized by South Lanarkshire Council after the outbreak and are still withheld from sale.
This case was brought against the company by South Lanarkshire Council and Food Standards Scotland.
Sheriff Weir rejected the council's application to destroy the company's entire stock of cheese but ruled that four batches of cheeses made with raw milk - one Lanark Blue and three Corra Linn - contained levels of Shiga toxin-producing E.coli (STEC).
The council said this made them potentially unfit to eat.
But while Sheriff Weir said these four batches should be destroyed, he said there was "no justification" for condemning all 83 batches of Lanark Blue.
Sheriff Weir said: "I am satisfied that Lanark Blue...was 'produced, processed or distributed' in compliance with the hygiene regulations."
He also ruled said there was no justification for the destruction of the stocks of Corra Linn cheese.
Last October the Crown Office said Errington Cheese would not face prosecution over the child's death in September 2016.
The decision comes after the firm's Dunsyre Blue was named the most likely source of the outbreak.
Errington Cheese welcomed the ruling in relation to its sheep's milk cheese and said it had never indicated that it would put the four batches of cheese on the market.
A statement on its website, from company owner Humphrey Errington, said: "The reason for pursuing this through the courts was the accusation that a years worth of cheese was unsafe and complete shut down of our business and livelihood rather than allowing me the option of disposing of these four batches and selling the 80 batches of Lanark Blue and 70 batches of Corra Linn following testing.
"I do believe South Lanarkshire Council would have been willing to discuss this rationally, however unfortunately it was clear to me their hands were tied by Food Standards Scotland who did not wish to discuss the possibility.
"As Food Standards Scotland are the 'Competent Authority' South Lanarkshire Council were obliged to follow their direction."
The company claimed the case, which is believed to have cost the council more than £500,000, could have been avoided if Food Standards Scotland were prepared to meet and discuss the case "in a rational manner".
The statement concluded: "We would like to express our profound gratitude to all our experts and all those who have so generously supported us through this process.
"I really hope that no other small business will have to go though such a harrowing and stressful process."
'Not taken lightly'
Ross Finnie, chairman of Food Standards Scotland said he was "disappointed" the Sheriff did not rule that all the sheep's milk cheeses should be destroyed.
He said: "Food Standards Scotland is content that the Sheriff's judgment in the case of South Lanarkshire Council v Errington Cheese Ltd concluded that some of the cheeses produced by Errington Cheese Ltd were unfit for human consumption.
"However, we are disappointed this did not include all of the cheese involved in these proceedings.
"Food Standards Scotland was part of the incident management team which worked to contain the E.coli O157 outbreak in Scotland in 2016 and which gathered the large volume of evidence required in this complex case.
"South Lanarkshire Council's decision to bring condemnation proceedings, which Food Standard Scotland supported, was not taken lightly and all the organisations involved believed there was a strong case for taking such action."
He said Food Standards Scotland would "study the judgment fully to consider its implications."
Michael McGlynn, South Lanarkshire Council's executive director of community and enterprise resources, said: "The council acknowledges the sheriff's decision to confirm the destruction of some of the cheese that action was taken against, whilst noting the sheriff's decision on the remaining batches.
"We are considering the implications of the decision."