The Iraq war is over. Buried by the news from Libya, Barack Obama announced late on Friday that all US troops will leave Iraq by 31 December.

The president put a brave face on it, claiming he was fulfilling an election promise to end the war, though he had actually been supporting the Pentagon's effort to make a deal with Iraq's prime minister Nouri al-Maliki to keep US bases and several thousand troops there indefinitely.

The talks broke down because Moqtada al-Sadr's members of parliament and other Iraqi nationalists insisted that US troops be subject to Iraqi law. In every country where they are based the US insists on legal immunity and refuses to let troops be tried by foreigners. In Iraq the issue is especially sensitive after numerous US murders of civilians and the Abu Ghraib scandal in which Iraqi prisoners were sexually humiliated. In almost every case where US courts tried US troops, soldiers were acquitted or received relatively brief prison sentences.

The final troop withdrawal marks a complete defeat for Bush's Iraq project. The neocons' grand plan to use the 2003 invasion to turn the country into a secure pro-western democracy and a garrison for US bases that could put pressure on Syria and Iran lies in tatters.

Their hopes of making Iraq a democratic model for the Middle East have been tipped on their head. The instability and bloodshed which the US unleashed in Iraq were the example that Arabs sought to avoid, not emulate. This year's autonomous surge for democracy in Egypt and Tunisia has done far more to galvanise the region and undermine its dictatorships than anything the US did in Iraq. And when the Arab spring dawned, the Iraqi government found itself on the defensive as demonstrators took to the streets of Baghdad and Basra to protest against Maliki's authoritarianism and his government's US-supported clampdown on trade union activity. Maliki hosted two Syrian government delegations this summer and has refused to criticise Bashar al-Assad's shooting of protesters.

But the neocons' biggest defeat is that, thanks to Bush's toppling of Saddam Hussein, Iran's greatest enemy, Tehran's influence in Iraq is much stronger today than is America's. Iran does not control Iraq but Tehran no longer has anything to fear from its western neighbour now that a Shia-dominated government sits in Baghdad, made up of parties whose leaders spent long years of exile in Iran under Saddam or, like Sadr, have lived there more recently.

The US Republicans are accusing Obama of giving in to Iran by pulling all US troops out. Their knee-jerk reaction is rich and only shows the bankruptcy of their slogans, since it was Bush who gave Tehran its strategic opening by invading Iraq, just as it was Bush in the dying weeks of his presidency who signed the agreement to withdraw all US troops by the end of 2011, which Obama was hoping to amend. But Senator John McCain was right when he said Obama's announcement would be viewed "as a strategic victory for our enemies in the Middle East, especially the Iranian regime, which has worked relentlessly to ensure a full withdrawal of US troops from Iraq". A pity that he did not pin the blame on Bush (and Tony Blair) who made it all possible.

The two former leaders' memoirs show they have learnt no lessons, even though their reputations in history will never be able to shake the disaster off.

Whether the lessons have been taken on board by the current US and British leaders is more important. Nato's relative success in the Libyan campaign is already being used to draw a veil over the past. Indeed, the fortuitous timing of Gaddafi's death has knocked the news of the US withdrawal from Iraq almost entirely off the media's agenda.

But the past is still with us. A key lesson from Iraq is that putting western boots on the ground in a foreign war, particularly in a Muslim country, is madness. That point seemed to have been learnt when US, British and French officials asked the UN security council in March to authorise its campaign in Libya. They promised there would be no ground troops or occupation.

This should also apply to Afghanistan where Obama claims to be fighting a war of necessity, unlike the war in Iraq which he calls one of choice. The distinction is false, and the question now is whether he will pull all US troops out by 2014.

On the pattern of the aborted deal with Iraq, his officials are trying to negotiate an arrangement with the Karzai government which will authorise the indefinite basing of thousands of US troops, to be described as trainers and advisers, after combat forces leave. This would continue the folly of fuelling the country's long-running civil war. Now that al-Qaida has been driven from Afghanistan, Washington should support negotiations for a government of national unity that includes the Taliban and ends the fighting among Afghans. Iraq is no haven of guaranteed stability but, without the presence of US combat troops for the last 15 months, it has achieved an uneasy peace. If talks in Afghanistan are seriously encouraged, it could go the same way once foreign troops at last withdraw.