A federal judge on Thursday ordered the military to stop touching the groins of detainees at the prison at Guantánamo Bay, Cuba, when they are moved from their cells to speak with lawyers. The procedure had led some prisoners to stop meeting with or calling their lawyers.

In a 35-page opinion, Judge Royce C. Lamberth, the chief judge of the Federal District Court for the District of Columbia, called the searches — which included guards wedging their hands between the genitals and thighs of the detainees as many as four times when moving them to a meeting and back to their cells — “religiously and culturally abhorrent” to Muslims. He portrayed the procedure as unnecessary and intended to “actively discourage” meetings with lawyers.

He said the warden, Col. John Bogdan, must return to a longtime procedure in which guards shake the underwear of detainees by the band to dislodge any contraband, but do not to touch their buttocks or genitals.