Detroit Mayor Kwame Kilpatrick has agreed to plead guilty to obstruction charges and will step down after more than six years leading the 11th-largest U.S. city.

The plea deal was announced during a court hearing Thursday and brings to an end a seven-months-long sex scandal that led to felony charges against Kilpatrick and plunged the city, region and state into political chaos.

The 38-year-old Democrat is expected to serve jail time as part of the plea deal.

The married mayor and former top aide Christine Beatty were charged in March with perjury, misconduct and obstruction of justice. They are accused of lying under oath about an affair and their roles in the firing of a deputy police chief.

Beatty did not plead guilty and next will appear in court on Sept. 11.