Spain's Supreme Court has ruled that the remains of dictator Francisco Franco should be exhumed.
It backed the Socialist government's plan to move the remains from a state mausoleum to a less controversial site.
An appeal by Franco's family against the exhumation and proposing an alternative site was rejected.
The issue has divided opinion in Spain, which remains haunted by the Franco era. He won the 1930s civil war and went on to rule Spain until 1975.
In a unanimous ruling, the court said it had decided to "completely reject the appeal lodged by the family in relation to Francisco Franco's exhumation".
In a tweet (in Spanish), Prime Minister Pedro Sanchez hailed the decision as a "great victory for democracy".
Mr Sanchez's deputy, Carmen Calvo, said the exhumation would be completed "as soon as possible". The government wants to carry it out before elections on 10 November.
Franco currently lies in a huge mausoleum called the Valley of the Fallen, alongside tens of thousands of civil war dead.
Many revile the complex - just outside Madrid - as a monument to the triumph of fascism, and it has become a shrine for the far right.
The government approved the exhumation in August.
It plans to put him next to his wife in El Pardo cemetery north of Madrid, where various other politicians are interred.
Many descendents of Franco's victims support the move.
"The idea that people who were killed by Franco's troops are buried together with Franco, it's very absurd, and they're still glorifying him as if he was the saviour of Spain," Silvia Navarro, whose great uncle died in 1936, told the BBC.
But the family, who would rather he was not moved at all, wanted him to lie in a family crypt in the Almudena Cathedral - right in the centre of the capital.
The government argued that the former dictator should not be placed anywhere where he could be glorified. It also said there were potential security issues with the cathedral site.
The controversy comes at a time of political crisis in Spain, as the country prepares for its fourth general election in four years.
Possible boost for the Socialists
By Guy Hedgecoe, BBC News, Madrid
This was a resounding ruling in favour of the Spanish government, with the Supreme Court unanimously approving the exhumation.
Just as noteworthy was the court's rejection of a claim by the Franco family that if the exhumation should go ahead, the remains should be reburied in a crypt beneath Almudena Cathedral in central Madrid.
Although the family could now go to the Constitutional Court, many observers believe another appeal would not succeed. The government, which has seen its plan to exhume Franco repeatedly delayed, will now aim to carry it out as soon as possible.
There are some, relatively minor, hurdles still to overcome, such as securing the Catholic Church's co-operation, but if Franco's remains are moved by 10 November it would give the Socialists a boost in the general election to be held that day.
What has been the reaction to the ruling?
Mr Sanchez said the government had always been guided by the determination to alleviate the suffering of Franco's victims.
"Today is a great victory of Spanish democracy," he said.
"The Supreme Court has endorsed the exhumation of Franco's remains and his transfer to El Pardo. Justice, memory and dignity."
Pablo Iglesias of the leftist Podemos party said the move was a "very important step" to remove a shame which had been present despite 40 years of democracy.
But Santiago Abascal, leader of the far-right Vox party, said he would oppose the Supreme Court decision "because only Vox has the courage to defend freedom and common sense from totalitarianism and electoral propaganda tricks".
Meanwhile, a spokesman for the main opposition Popular Party said he did not intend to devote any time to the question.
"I have no opinion... I respect the procedures of the courts," Alfonso Serrano said, quoted by Efe news agency.
How has Spain dealt with the Franco era?
Unlike in Mussolini's Italy and Nazi Germany, defeated in World War Two, Spain's transition to democracy in 1975 was more gradual.
Though democracy is well established now, many believe the country has never faced up to its fascist past. There was an unwritten "pact of forgetting" during the transition.
An Amnesty Law adopted in 1977 prevents any criminal investigation into the Franco years.
Statues of Franco were removed and many streets were renamed, to erase obvious signs of the fascist past.
A Historical Memory Law, passed in 2007 by the socialist government at the time, recognised the war victims on both sides and provided some help for surviving victims of Franco's dictatorship and their families.
But the work to locate and rebury thousands of civil war dead has been slow and controversial.
More than 100,000 victims of the conflict, and the ferocious repression carried out afterwards, are still missing.
Who was Francisco Franco?
- Becomes youngest general in Spain in 1926, aged 33
- After election of leftist Popular Front in 1936, Franco and other generals launch revolt, sparking three-year civil war
- Helped by Nazi Germany and Mussolini's Italy, Franco wins civil war in 1939 and establishes a dictatorship, proclaiming himself head of state - "El Caudillo"
- Franco keeps tight grip on power until his death in 1975, after which Spain becomes a democracy