Image copyright AP Image caption The diplomat was appointed by David Cameron in 2013

The UK's ambassador to the EU, Sir Ivan Rogers, has resigned.

Sir Ivan, appointed to the job by David Cameron in 2013, had been expected to play a key role in Brexit talks expected to start within months.

The government said Sir Ivan had quit early so a successor could be in place before negotiations start.

Last month the BBC revealed he had privately told ministers a UK-EU trade deal might take 10 years to finalise, sparking criticism from some MPs.

Ministers have said a deal can be done within two years.

Labour said Sir Ivan's departure was "deeply worrying" and Prime Minister Theresa May must be prepared to listen to "difficult truths" about the likely complexity of the Brexit process.

The diplomat was due to leave his post in November.

Media playback is unsupported on your device Media caption Diplomat's exit 'wise decision' says Redwood

A government spokeswoman said: "Sir Ivan Rogers has resigned a few months early as UK permanent representative to the European Union.

"Sir Ivan has taken this decision now to enable a successor to be appointed before the UK invokes Article 50 by the end of March. We are grateful for his work and commitment over the last three years."

Analysis - by Kevin Connolly, BBC Europe correspondent

The official UK statement announcing the departure of Sir Ivan Rogers makes it sound like a routine piece of bureaucratic house-keeping - bringing forward the departure of the current incumbent to give his successor time to get his teeth into Brexit.

It seems certain there's more to it than that.

Sir Ivan endured an uncomfortable time at the last EU summit in December after his confidential advice to the government about Brexit potentially taking 10 years was leaked to the BBC. No ambassador relishes "becoming the story" in that way.

There's a sense in Brussels that he may have been seen as a pessimist by cabinet Brexiteers because it fell to him to convey the hostility and scepticism with which other governments view Brexit.

One British insider here, though, called him a key adviser who'd be a huge loss.

Whoever replaces him faces one of the trickiest British diplomatic assignments of modern times.

Prime Minister Theresa May says she will trigger formal talks between the UK and the EU by the end of March, setting in place a two-year negotiation process.

Sir Ivan is a veteran civil servant whose previous roles include private secretary to ex-chancellor Ken Clarke, principal private secretary to ex-PM Tony Blair and Mr Cameron's Europe adviser.

He was criticised in some quarters for "pessimism" over Brexit after his advice to ministers - which he said reflected what the 27 member states were saying - was reported.

Pro-EU figures raised concern about the impact of Sir Ivan's departure, while Brexit campaigners welcomed his decision. Former Lib Dem leader Nick Clegg, who once worked for Sir Ivan in Brussels, described his resignation as a "body blow to the government's Brexit plans".

He added: "If the reports are true that he has been hounded out by hostile Brexiteers in government, it counts as a spectacular own goal."

Media playback is unsupported on your device Media caption Hilary Benn tells the World at One that "having a handover in the middle of our negotiations is not ideal"

Labour MP Hilary Benn, who chairs the Brexit select committee, said it had come at a "crucial" point and urged the government to "get its skates on" in finding a replacement. "It couldn't be a more difficult time to organise a handover," he added.

Mr Benn told BBC Radio 4's The World at One the permanent representative's job was to convey the view of the UK to other member states, as well as "honestly and fearlessly reporting back" what those countries in turn said about the negotiations.

But former Conservative cabinet minister John Redwood said Sir Ivan had made a "very wise decision", saying his leaked advice suggested he did "not really have his heart in" Brexit, believing it to be "very difficult and long-winded".

He said the new ambassador should be someone "who thinks it's straightforward".

Former UKIP leader Nigel Farage said he welcomed Sir Ivan's resignation, adding: "The Foreign Office needs a complete clear-out."

But former chancellor George Osborne said Sir Ivan was a "perceptive, pragmatic and patriotic public servant" while the Treasury's former top civil servant, Lord Macpherson - who is now a crossbench peer - said his departure marked a "wilful" and "total destruction" of EU expertise within Whitehall.