Kenya’s High Court on Thursday overturned a government order to close the world’s biggest refugee camp, home to more than 300,000 Somali refugees, including some who have lived there more than 20 years.

The Kenyan government announced in 2015 it would close the Dadaab refugee camp, sending many people back to Somalia, arguing the camp was "an existential security threat to Kenya,” and a “nursery for Al Shabab,” the Al Qaeda linked Somali extremist group.

The court ruling keeps the camp open and brings more certainty for refugees afraid of being repatriated to Somalia, where large parts of the south are controlled by the Shabab and a famine is looming.

Human rights groups Amnesty International, Kituo Cha Sheria and the Kenya National Commission on Human Rights launched the court challenge against the closure.

In a toughly worded judgment, Judge John Mativo ruled Thursday that the interior minister and other officials had exceeded their powers and violated the law in their decision to close the camp, which he found would amount to group persecution of Somali refugees. He also found that the move would flout Kenya’s international treaty obligations on refugees.

"The government's decision specifically targeting Somali refugees is an act of group persecution, illegal, discriminatory and therefore unconstitutional,” Mativo said.

The judge also ruled out the the repatriation of refugees to Somalia, since Kenya had not shown it was safe for refugees to return.

In Somalia, the Shabab has recently beheaded people it accused of collaborating with Ethiopian forces. It has killed those who cooperated with the Somali army or allied United Nations-backed African forces, the African Union Mission to Somalia. It has assassinated people working for the government and in rural areas has killed people who disobeyed its harsh rules, such as impoverished people who burned charcoal to survive.

The Shabab forcibly recruits youths to fight and levies heavy taxes in areas it controls, particularly on traders and transport operators.

Human rights groups welcomed the decision to overturn the closure, but the government said it would appeal the decision.

"For us as a government, Kenya will always come first. The lives of Kenyans matter. It is for this reason that we shall be strongly appealing the decision by the High Court,” government spokesman Eric Kiraithe told journalists Thursday in Nairobi.

Kenya has seen many gruesome terrorist attacks by the Shabab, including the 2013 Westgate shopping mall attack in the capital, Nairobi, which killed at least 67, and the 2015 attack on a university in the northern town of Garissa, which killed at least 147.

The government claims both attacks were planned by terrorists living in the Dadaab camp.

In Kenya, the Shabab often spares Muslims in attacks, although its victims in Somali attacks are often Muslim civilians.

The Kenyan government frequently targets ethnic Somalis in security raids, particularly in the Somali neighborhood of Eastleigh, near Nairobi. In 2014, Kenya rounded up more than 1,000 Somalis, including women and children, detained them in a football stadium and deported those without documents.

Dadaab, a series of sprawling settlements as big as a town, sprang up after Somalia descended into war and famine in the early 1990s after the dictatorship of Mohamed Siad Barre fell in 1991. Some Somalis have lived in Dadaab for more than two decades, and many were born there.

But the “strategy for mass withdrawal” agreed on at the African Union summit in Addis Ababa, Ethiopia, is not binding, and individual...

African leaders have agreed to a plan for a mass withdrawal from the International Criminal Court, amid criticisms its prosecutions focused too heavily on Africans.

But the “strategy for mass withdrawal” agreed on at the African Union summit in Addis Ababa, Ethiopia, is not binding, and individual...

(Robyn Dixon)

The camp is situated in a harsh location in remote eastern Kenya, with temperatures as high as 118 degrees in the dry season and floods in the wet season.

Somalis have continued to flee to Kenya since 1991 as stability has eluded their country, ravaged by famines, droughts, clan warfare and the Shabab. The last big influx of refugees was in 2011, when a famine drove thousands from their homes and some 260,000 people died.

Another famine is looming in Somalia, U.N. agencies have warned, threatening another massive exodus.

Half a world away in U.S. federal court, a three-judge panel Thursday upheld a lower-court ruling suspending the enforcement of a Trump administration travel ban. Somalia is among seven Muslim-majority countries affected by the ban.

The ban caused anxiety among Somalis in Dadaab who had been granted entry to the U.S. Dozens of people who were waiting in Nairobi for a flight to the U.S. were sent back the Dadaab camp, according to the Associated Press, although legal barriers to their entry have since been cleared for now.