Former SNP leader Alex Salmond has resigned from the party amid allegations of sexual misconduct.
In a statement he said he wanted to avoid internal division within the SNP, which has faced calls to suspend him.
He has denied any wrongdoing, and said he intended to apply to rejoin once he had an opportunity to clear his name.
It emerged last week that two Scottish government staff members had lodged complaints in January about his behaviour when he was first minister.
Mr Salmond has described the allegations as "patently ridiculous" - and has also criticised the complaints procedure which he claims is "unjust".
On Tuesday he formally began legal action against the Scottish government in the Court of Session over its handling of the misconduct allegations.
In a statement released on social media, Mr Salmond said he had been a member of the SNP for 45 years, 20 of them as party leader and seven as first minister.
He continued: "I truly love the SNP and the wider independence movement in Scotland. They have been the defining commitment of my life. But today I have written to the National Secretary of the party resigning my membership."
Mr Salmond indicated that his resignation was to avoid potential divisions within the party, as his successor Nicola Sturgeon faced opposition calls to suspend his SNP membership.
He stated: "I did not come into politics to facilitate opposition attacks on the SNP and, with Parliament returning next week, I have tendered my resignation to remove this line of opposition attack.
"Most of all, I am conscious that if the party felt forced into suspending me it would cause substantial internal division."
The current SNP leader and first minister, Nicola Sturgeon, said she "felt a huge sadness about the whole situation".
In a statement on Twitter, she said the decision was Alex Salmond's alone, and she understood why he had chosen to separate "the current questions he is facing from the day to day business of the SNP and the ongoing campaign for independence".
She continued: "The hard fact remains that two complaints were received by the Scottish government that could not be ignored or swept under the carpet."
The Daily Record newspaper broke the news of the sexual misconduct allegations last Thursday.
The paper claims to have seen the wording of one complaint which describes an incident alleged to have taken place at the first minister's official residence in Edinburgh, Bute House, in the first week of December 2013.
The two women lodged complaints in January this year, just weeks after the Scottish government adopted a new complaints procedure in the light of wider concern about sexual harassment at Holyrood and Westminster.
Mr Salmond claims that the subsequent investigation into the allegations against him by senior Scottish government civil servants was "unfair and unjust".
He said he had been given no opportunity to "see and therefore to properly challenge the case against me" and that he had "not been allowed to see the evidence".
Mr Salmond has also claimed that someone within the Scottish government has "flagrantly and repeatedly" breached the confidential complaints process by leaking details to the Daily Record.
He is now seeking a judicial review of the new complaints procedure and has launched a crowdfunding appeal to fund his legal action.
- Leader of Scottish National Party 1990-2000 and 2004-2014
- First Minister of Scotland 2007 - 2014
- Stood down after Scotland voted to remain in the United Kingdom
The Scottish government has insisted the complaints process has been "entirely confidential throughout" and has said it will defend its position vigorously in the courts.
The complaints against Mr Salmond have also been passed to Police Scotland which has said it is assessing the information.
Mr Salmond was Scottish first minister and SNP leader until September 2014 when he resigned following the "No" vote in the Scottish independence referendum.
He returned to frontline politics when he was elected as the SNP MP for Gordon in 2015 and became the party's foreign affairs spokesman at Westminster before losing the seat in the snap general election two years later.
Since then he has faced heavy criticism for hosting the Alex Salmond Show on Russian broadcaster RT, which has been described as a propaganda channel for the Kremlin - a claim Mr Salmond has denied.