MEXICO CITY — The raid delivered a resounding blow to Mexico City’s most fearsome criminal group: 31 suspected gang members were arrested, two drug labs were uncovered and a criminal bounty was seized, including two and a half tons of marijuana, 44 pounds of cocaine and an arsenal of weapons.

Within a few days, however, the case had collapsed. A judge, finding inconsistencies in the prosecution’s files, freed nearly all of the suspects. And a major victory for the government turned into a major embarrassment.

The outcome was, in some ways, a reaffirmation of Mexico’s new criminal justice system. A sweeping overhaul of the judiciary, fully carried out in 2016, has created more transparency in the courts, raised the standards of evidence and given judges more authority to toss out charges because of procedural errors.

But with this shift to stronger due process, government officials have come to a difficult realization: An overhaul meant to strengthen the nation’s ability to combat violence and impunity has also made it more difficult to put people behind bars and keep them there.