WASHINGTON — President Trump once again berated the “dirty cops” of the law enforcement establishment on Thursday, accusing the Justice Department of going after his friends but not his enemies in an outburst that flouted Attorney General William P. Barr’s pleas to stop publicly intervening in prosecutions where he had a personal interest.

Speaking out hours after his friend Roger J. Stone Jr. was sentenced to more than three years in prison for lying to protect the president, Mr. Trump belittled the case and hinted broadly that he would use his clemency power to spare Mr. Stone if a judge did not agree to a retrial sought by defense lawyers.

In essentially dangling a pardon or a commutation for a friend, Mr. Trump confronted Mr. Barr with a choice about how to respond after he declared last week that the president’s attacks on the criminal justice system were making his job “impossible.”

“A lot of bad things are happening, and we’re cleaning it out,” Mr. Trump said of law enforcement at a Las Vegas event for former convicts re-entering society. “We’re cleaning the swamp. We’re draining the swamp. I just never knew how deep the swamp was.”