Kevin Garratt, the Canadian held for two years in China on charges of spying and stealing state secrets, is back home in Vancouver after a judge ordered his deportation.

According to an emailed statement from the Garratt family, a Chinese court in the border city of Dandong handed down its ruling on Tuesday. Two days later, he was deported and "has returned to Canada to be with his family and friends."

Garratt, right, sits with Sean Robertson, an official from the Canadian Embassy in Beijing, aboard a flight from Shenyang, China, to Tokyo, Japan. (Provided by Garratt family)

"The Garratt family thanks everyone for their thoughts and prayers, and also thanks the many individuals who worked to secure Kevin's release," the statement said. "Please respect the family's privacy in this time of transition. We will be releasing more information in the coming weeks."

The family also released photos showing Garratt on his way back to Canada.

A statement issued Friday by China's Ministry of Foreign Affairs said Garratt was allowed to leave after the court released its decision.

While the statement didn't give the verdict, CBC's Sasa Petricic reports that Garratt was found guilty on two counts of espionage, and the judge ordered him deported.

Petricic said Canadian officials were allowed to be at the hearing, which was not the case at earlier court appearances. They then accompanied him to Vancouver via Tokyo.

"Sources in Beijing say a major irritant has now been removed from the relationship between China and Canada," Petricic reported.

Garratt's release came just over a week after Prime Minister Justin Trudeau returned from his first official visit to China.

During a press conference with Trudeau in Bejiing on Aug. 31, Chinese Premier Li Keqiang was asked about Garratt's case. Li responded that he would be treated humanely and his case would be handled in accordance with Chinese law.

It was announced this week that Li will be in Canada next week for an official visit, and is scheduled to meet with Trudeau in Ottawa.

Chinese Premier Li Keqiang, right, was asked about Garratt's case during a press conference with Prime Minister Justin Trudeau in Beijing last month. Li makes an official visit to Canada next week. (Thomas Peter/Reuters)

Trudeau welcomed Garratt's release in a statement Thursday, saying the government had been "seized" by the case "at the highest levels."

"We are delighted that Kevin Garratt has returned safely to Canada and is with his family once more," the release quoted Trudeau as saying. "We remain deeply impressed by the grace and resilience of the Garratt family, especially Kevin and Julia.

"We want to thank consular officials who work behind the scenes every day in support of Canadians abroad."

Accused of spying

Garratt and his wife, Julia, who are from Vancouver and had lived in China since 1984, ran a popular coffee shop in the city of Dandong, near China's sensitive border with North Korea, and carried out Christian aid work in the area. They were detained in August 2014 and accused of stealing Chinese military secrets.

Julia Garratt was released on bail in February 2015 and returned to Canada, but Kevin Garratt remained in detention.

In January of this year, a Chinese Foreign Ministry spokeswoman said authorities there had found evidence that "implicates Garratt in accepting assignments from Canadian espionage agencies to gather intelligence in China."

Their family denied accusations the couple had been involved in espionage, and the government has said there was no evidence to back the Chinese claims.

In fact, Canada's top spy travelled to China four months ago to meet with his Chinese counterpart, Geng Huichang, CBC News has confirmed.

In an unusual move, CSIS Director Michel Coulombe told Chinese officials directly that Garratt did not work for the spy agency.

The Garratts' initial detention followed closely on accusations by the Canadian government that China had spied on Canadian federal agencies, including the National Research Council.

During his China trip, a few days after his meeting with Li, Trudeau was asked how Canada could justify continuing discussions on trade and closer relations while Garratt was being held on charges Canada had said weren't based on evidence. He said Canadian officials had been working "diligently" on Garratt's case and on "other challenging issues."

"Having a strong relationship with China means being able to benefit our country but also being able to stand up and defend our citizens' interests. That's what the Chinese government expects and that's exactly what Canadians expect of our government," Trudeau told reporters in Hanghzou, China.

Former prime minister Stephen Harper also raised the Garratts' case during his visit to China in late 2014.