A Boston federal court judge has ordered U.S. Immigration and Customs Enforcement officials to stop arresting undocumented immigrants who show up for court dates in Massachusetts, granting a preliminary injunction in immigration advocates’ case against federal agencies.

“Criminal defendants will be unable to vindicate their rights if they are taken into ICE custody prior to appearing in court or if witnesses in their defense are too fearful to visit a courthouse," U.S. District Court Judge Indira Talwani wrote in an decision issued Thursday. “None of these harms can be remedied after the conclusion of this litigation. Therefore, the court finds that the Plaintiffs have alleged irreparable harm sufficient to warrant and injunction.”

Talwani ordered that ICE and the U.S. Department of Homeland Security stop arresting immigrants who enter Massachusetts courts on “official business” as defendants or witnesses in a case while they are going to, attending, or leaving the courthouse. The ruling does not apply to undocumented people who are brought to courts in federal or state custody.

At a May 23 hearing, Talwani asked the government if the immigrants arrested had criminal warrants out for their arrest.

Erez Reuveni, who is representing the government, said usually they do not, but then said he was unsure and did not have the data. He said ICE focuses only on people who pose a national security or public safety threat when they take the step of a courthouse arrest.

Immigration advocates, however, point to a policy on ICE’s website that states that courtrooms are not considered “sensitive locations” where immigrants may be protected from arrest.

In April, the Middlesex County District Attorney’s Office announced a group of prosecutors, public defenders and community groups filed suit over courthouse arrests. They argue that civil arrests deter victims, witnesses and defendants from cooperating with law enforcement on local criminal matters.

The suit argues that the practice has a chilling effect on prosecution in the commonwealth, where people without proper documentation may be afraid to report crimes or participate in the justice system for fear of arrest. Lawyers for Civil Rights, also a party in the suit, has tracked more than 100 arrested by ICE agents at local courthouses in the past year.

Suffolk County District Attorney Rachael Rollins, the Committee for Public Counsel Services and Chelsea Collaborative joined Middlesex District Attorney Marian Ryan and Lawyers for Civil Rights in the suit.