OTTAWA -- The Supreme Court of Canada says the trustee for a bankrupt Alberta energy company cannot simply walk away from unprofitable wells on agricultural land without having to clean up.

The high court's 5-2 ruling comes with a recommendation from Chief Justice Richard Wagner for Parliament to clarify the confusion between the federal bankruptcy law and the regulations provinces rely on to protect the environment.

"Bankruptcy is not a licence to ignore rules, and insolvency professionals are bound by and must comply with valid provincial laws during bankruptcy," Wagner wrote.

The effect will be to reduce what creditors of bankrupt companies can get back when the companies' assets are liquidated, because some of the proceeds will have to go to environmental cleanups instead of to repaying loans.

In Calgary Thursday, Alberta Energy Minister Marg McCuaig-Boyd praised the court's decision and said it gives the province more clout.

"We're pleased that they've upheld the polluter-pay principle," said McCuaig-Boyd. "It does give clear direction that companies that go bankrupt don't get to walk away from their responsibilities."

McCuaig-Boyd said the province continues to work with industry and has been closing loopholes and tightening the rules while it explores long-term solutions, including hard timelines and targets on when companies have to act to clean up orphan wells.

"There will be more onus on people to clean up. In the past we've made it easier to just pay rent rather than clean up (the wells)," said McCuaig-Boyd.

She said the province's energy regulator also has been given more legal authority to stop companies from abandoning a well only to "pop up as another company elsewhere."

She said 2,400 wells have already been cleaned up, adding that most energy companies are responsible and that the problem is tied to a small number of "bad actors."

The opposition Alberta party's leader Stephen Mandel said in a statement that although the decision will likely make it more difficult for oil and gas companies to borrow money, "we also believe that this is the right decision for our environment and that producers must fulfill their environmental obligations to Albertans for reclamation and remediation."

Thursday's decision overturns an Alberta Court of Appeal ruling that upheld a 2016 decision in the Alberta Court of Queen's Bench that allowed a bankrupt energy company to sever its connection with unprofitable and unreclaimed wells when the company's assets were sold off to creditors, as if the wells were debts that the company couldn't cover.

The Supreme Court ruled that Redwater Energy Corp.'s bankruptcy trustee, Grant Thornton Ltd., cannot walk away from the company's obligations to render abandoned wells environmentally safe.

When Redwater went bankrupt, Alberta's provincial energy regulator ordered Grant Thornton to comply with end-of-life requirements to render Redwater's abandoned properties environmentally safe.

The trustee did not comply, and filed its own counterclaim that included a challenge to the regulator's action, citing the paramountcy of federal bankruptcy law.

Alberta's energy regulator and the Orphan Well Association, an industry-funded group that cleans up wells that nobody else will, appealed the ruling to the high court.

"The end-of-life obligations the Regulator seeks to enforce against Redwater are public duties. Neither the regulator nor the Government of Alberta stands to benefit financially from the enforcement of these obligations," Wagner wrote. "These public duties are owed, not to a creditor, but, rather, to fellow citizens, and are therefore outside the scope of 'provable claims'."

Since the case went to court in 2016, an estimated 1,800 wells representing more than $100 million in liabilities have been abandoned.

The Orphan Well Association's last annual report said low oil prices had meant a sharp increase in the number of unprofitable wells it has had to take on from bankrupt companies. In 2012, it had 74 dud wells in its inventory; by the end of 2017, the number was 1,778.

"The work we do varies from straightforward to highly complex," that report said. "On one side of that spectrum is restoring the land on an orphan property that may, for example, have a low impact in a rural area. On the more complex side, we have safely decommissioned wells with potentially dangerous levels of hydrogen sulphide gas near more densely populated residential areas."

Orphaned wells are often on rented agricultural land and a group with the support of thousands of farmers also wanted to see the high court reverse the decision. The Action Surface Rights Association intervened in the case because it believes rights of landowners have been overlooked in the case.

Saskatchewan's government welcomed the decision, too, saying it has 249 orphan wells and a program similar to Alberta's for dealing with them.

"The Court has made it clear that in the case of receiverships and bankruptcies, the environmental obligations of oil and gas companies take precedence over a company's other debts," the province's Ministry of Energy and Resources said in a statement.

The case featured a novel legal twist.

The previous Alberta appeal court decision that favoured Redwater and Grant Thornton was not unanimous -- one judge dissented, Justice Sheilah Martin.

Martin has since been appointed to the Supreme Court. But she did not participate in Thursday's case.

Nonetheless, Martin's previous ruling guided the Supreme Court's ruling on how the key section of the federal bankruptcy act -- section 14.04(4) -- ought to be interpreted.

Martin interpreted Parliament's intent in writing the law as "aimed at concerns about the protection of trustees, not the protection of the full value of the estate for creditors," Wagner wrote. "I agree with Martin ... that there is no basis on which to read the words 'the trustee is not personally liable' in s.14.04(4) as encompassing the liability of the bankrupt estate."

With files from Dean Bennett in Edmonton.