Then-acting FBI director Andrew McCabe testifies at a U.S. Senate Select Committee on Intelligence hearing in May 2017. (Jahi Chikwendiu/The Washington Post)

Andrew McCabe, the acting FBI director who authorized an investigation into President Trump for ties to Russia and possible obstruction of justice, filed a lawsuit against the bureau and the Justice Department on Wednesday, alleging he was illegally demoted and fired as part of a plot by Trump to remove those who were not politically loyal to him.

McCabe asked that a federal judge declare his termination a “legal nullity” and essentially allow him to retire from the FBI as planned, with all the benefits that would have afforded him. He was fired from the bureau in March 2018, just hours before he was set to retire, costing him significant retirement benefits. The termination came after the Justice Department inspector general found that McCabe made an unauthorized disclosure to the media, then lied to investigators about it.

“It was Trump’s unconstitutional plan and scheme to discredit and remove DOJ and FBI employees who were deemed to be his partisan opponents because they were not politically loyal to him,” the lawsuit alleges, adding that McCabe’s firing “was a critical element of Trump’s plan and scheme.”

Justice Department and FBI spokeswomen declined to comment. White House officials did not immediately respond to a message seeking comment.

The inspector general referred McCabe’s case to the U.S. attorney’s office in Washington, which has been using a grand jury to determine whether McCabe should also be charged criminally. McCabe has long asserted that he did nothing wrong and that his termination, ordered by then-Attorney General Jeff Sessions, was a politically motivated effort by Trump to undermine the FBI’s work. Trump had criticized Mc­Cabe even before he was removed.

The suit is the second this week by former FBI officials who say they were wrongly removed from their positions for political reasons.

On Tuesday, former FBI agent Peter Strzok, who also played a key role in the Russia probe, sued the Justice Department and FBI for reinstatement and back pay, arguing he was unfairly terminated for criticizing Trump. Strzok was found to have sent anti-Trump text messages, which FBI Deputy Director David Bowdich said called into question the bureau’s decisions in the Russia probe and the separate investigation of Hillary Clinton’s use of a private email server while secretary of state.

McCabe’s lawsuit is notable for its forcefulness, alleging that Trump enlisted the highest-ranking members of the federal law enforcement apparatus in a scheme to stifle dissent. The suit singles out Sessions and FBI Director Christopher A. Wray, who it claims “knowingly acted in furtherance of Trump’s plan and scheme, with knowledge that they were implementing Trump’s unconstitutional motivations for removing Plaintiff from the civil service.”

“Trump demanded Plaintiff’s personal allegiance, he sought retaliation when Plaintiff refused to give it, and Sessions, Wray, and others served as Trump’s personal enforcers rather than the nation’s highest law enforcement officials, catering to Trump’s unlawful whims instead of honoring their oaths to uphold the Constitution,” the lawsuit alleges. “Plaintiff” refers to McCabe, who filed the suit.

The lawsuit traces the origins and evolution of McCabe and Trump’s relationship, starting when the FBI in 2016 publicly recommended closing its investigation into Clinton’s private email server. Soon after, the suit alleges, then-candidate Trump began to attack McCabe, taking aim at political donations McCabe’s wife received when she made a failed run for a state Senate seat in Virginia from a group controlled by a prominent Clinton supporter.

The suit claims that even though Trump ultimately won the election, he remained fearful that he was in political peril due to his “loss of the popular vote and his campaign’s acceptance of Russian assistance during the presidential election.”

“Once in office,” the suit alleges, “Trump began to purge the DOJ and FBI of officials whom he perceived as his partisan opponents rather than Trump loyalists, and as affiliated with the Democrats because of their support for the Russia investigation.”

The suit claims Trump pressured Sessions, then-Deputy Attorney General Rod J. Rosenstein and others to bend to his political will, and that he keyed in on Mc­Cabe and then-FBI Director James B. Comey, whom Trump fired May 9, 2017. The lawsuit does not identify others it claims were purged, though Trump also removed Deputy Attorney General Sally Yates days into his administration.

Trump cited the Russia investigation as a reason for firing Comey. McCabe alleged there was an effort to get him ousted from the case as well.

In one meeting just days after Rosenstein appointed Robert S. Mueller III to lead the Russia investigation, the suit alleges, Rosenstein broached McCabe’s wife’s political campaign and asked him to consider recusing himself. The suit alleges Rosenstein referenced a photo of Mc­Cabe wearing a campaign T-shirt, suggesting it could create a credibility issue and cause “unspecified others to complain about Plaintiff’s involvement in the Russia investigation.”

McCabe was then the acting FBI director, but he told Rosenstein and Sessions he intended to retire the following March when he became eligible, the suit alleges. He was soon replaced by Wray and returned to his post as the bureau’s No. 2 official.

“Plaintiff understood Rosenstein’s concern about unspecified third parties’ complaints to include the only officials who outranked Rosenstein in the DOJ chain of command: Trump and Sessions,” the suit alleges.

Neither Rosenstein nor a spokeswoman for Sessions addressed requests for comment.

Trump continued to attack McCabe publicly in the months that followed with a clear implication: He wanted McCabe gone. In August 2017, the suit alleges, Sessions asked Wray — at Trump’s urging — to fire McCabe, but Wray refused and “suggested that he would resign if Sessions continued to apply such pressure.”

Meanwhile, the inspector general was investigating McCabe for the media disclosure, and in December 2017, told Wray of a forthcoming report. The next month, citing that investigation, Wray gave McCabe a choice, the suit alleges: transition to a lesser role of his choosing and falsely announce he was stepping down voluntarily, or be reassigned to a lesser role of Wray’s choosing.

McCabe said he’d go on terminal leave until eligible to retire but “would not lie to the FBI workforce about the circumstances of his departure,” the suit alleges.

On March 7, 2018, the FBI’s assistant director of the Office of Professional Responsibility, Candice Will, recommended McCabe be fired over the inspector general’s findings. The lawsuit alleges that she seemed to be aware of top officials’ desire to remove McCabe before his expected retirement date that month because she attached a handwritten note to her recommendation saying, “It seems unlikely that [the proposed termination] will reach final resolution before Mr. McCabe’s March 18 retirement date, but that is up to the DAG.”

The lawsuit alleges officials expedited the process so that McCabe’s team had limited time to review the evidence against him, and that one Justice Department official conceded: “We’re making it up as we go along.”

The suit alleges McCabe learned from the media he was being fired, and notes that Trump celebrated the move on Twitter. It claims McCabe’s constitutional rights were violated.

After he was fired, McCabe wrote a book about his time in the FBI, which detailed his uncomfortable and unusual interactions with Trump and Sessions. He also raised more than $538,000 on a GoFundMe page set up for his legal defense.