WASHINGTON — A federal judge in Seattle on Tuesday blocked an order by Attorney General William P. Barr that would have kept thousands of migrants detained indefinitely while waiting for their asylum cases to be decided.

Judge Marsha J. Pechman of United States District Court for the Western District of Washington described the order, which would have denied some migrants a bail hearing, as unconstitutional. Under a preliminary injunction, Judge Pechman said migrants must be granted a bond hearing within seven days of a request or be released if they have not received a hearing in that time.

“The court finds that plaintiffs have established a constitutionally protected interest in their liberty, a right to due process, which includes a hearing before a neutral decision maker to assess the necessity of their detention and a likelihood of success on the merits of that issue,” Judge Pechman wrote.

Her ruling also noted that the government was likely to appeal the decision.

The order, which was issued in April, was another attempt by the Trump administration to prevent the release of migrants into the country — and to deter asylum seekers from crossing the border altogether.