WASHINGTON — President Trump surprised Donald F. McGahn II, the White House counsel, on Wednesday by abruptly announcing that Mr. McGahn will be leaving his job this fall, effectively forcing the long-anticipated exit of a top adviser who has cooperated extensively in the investigation into Russian election interference.

The president made the declaration on Twitter without first informing Mr. McGahn, according to people close to both men. It came 11 days after The New York Times reported the degree to which Mr. McGahn — who was by Mr. Trump’s side at major moments as the president sought to keep control of the Russia inquiry — has emerged as a key witness in the investigation. Over the past nine months Mr. McGahn has given 30 hours of testimony in at least three voluntary interviews.

Mr. McGahn’s departure leaves the White House without one of the few senior advisers who have been willing to push back against Mr. Trump. It also raised the prospect of further West Wing exits, particularly in the White House Counsel’s Office, where Mr. McGahn has had a loyal staff, with several people staying in their jobs out of devotion to him.

Mr. McGahn had long discussed his intention to leave at some point, but the way Mr. Trump blindsided him with a tweet underscored how dysfunctional the relationship had become, even as the Russia inquiry appears to be reaching a critical phase, and the legal scrutiny is mounting for the president.