Therefore, the true opinion is the fifth, according to which the Pope who is manifestly a heretic ceases by himself to be Pope and head, in the same way as he ceases to be a Christian and a member of the body of the Church; and for this reason he can be judged and punished by the Church.
A heretic "pope" loses his office ipso facto by divine law (a heretic ceases to be Christian and a member of the body of the Church). Then the Church can make it official with a declaration of that fact.
Per Roman Catholic Canon Law....
"As we all know, there is no higher-ranking official in the Catholic Church than the Pope. Canon 331 is unambiguous: the Pope alone has supreme, full, immediate and universal ordinary power in the Church. (See “Are there Any Limitations on the Power of the Pope?” for more on this topic.) Consequently, in the absence of any “special laws which have been enacted” to handle this particular situation, there is nobody on earth who has the authority to make an official determination that the Pope is incapacitated and must be removed, or that somebody else should henceforth govern in his place.
It simply cannot be done."
https://canonlawmadeeasy.com/2013/01/31/pope-removed-from-office/