The Latin American left has had a miserable few months. InArgentina, Venezuela and Bolivia, it was decisively defeated in three different kinds of elections. The president of Ecuador, Rafael Correa, most likely seeing the trends around him, decidedto abandon his attempts to remain in power. In Chile,corruption scandals are tightening around previously respected leaders. Most recently, the web of graft has caught one of the most revered figures among Latin America’s 21st-century leftists: the former president of Brazil, Luiz Inácio Lula da Silva.The “pink tide” is ebbing. Why has this happened now? And what can the left learn as it recedes from power?Since Hugo Chávez’s 1998 election in Venezuela, moderate or hard-line left-of-center parties, movements and leaders have ruled much of Latin America. Left-wing incumbents were re-elected, or their handpicked replacements won handily. With a few important exceptions, these administrations governed well, improved their constituents’ lives and enacted sensible macroeconomic policies.They were also lucky. From roughly 2003 through 2012, Latin America enjoyed one of the greatest commodity booms in its modern history. Exporting everything from oil to soybeans, Latin American governments received windfalls, which they spent on social programs, which were often well designed and affordable. The problem is that no one saved up for the inevitable rainy day. When prices began to plummet, both newsovereign wealth funds and traditional tactics, like fiscal stimulus, proved inadequate. Country after country saw growth rates fall, social spending shrink and citizens get angry.The recent defeats largely stem from this economic reality. But not exclusively. Too many Latin American leftist leaders fell prey to the region’s endemic corruption — and underestimated growing intolerance to it. By the time some governments, like Chile’s and Bolivia’s, started to focus on the issue, it was too late. They had become as deeply enmeshed in Latin America’s tradition of corrupt practices as their conservative predecessors, civilian or military, elected or imposed.The continuing scandal in Brazil spread from high-level officials in the state-owned oil company, the federal government and Congress to the former president and his family. President Dilma Rousseff could be the next to go down, thanks to the confessions of senators from her own political party and of João Santana, the political consultant who directed her and Mr. da Silva’s campaigns. It is increasingly likely that Mr. da Silva will be arrested and Ms. Rousseff will be impeached. But the implications go beyond Brazil’s borders: Mr. Santana also consulted on elections won by real or supposed left-wing candidates in Venezuela and Peru.There were other mistakes, too. While several leftist governments — in Chile, Brazil, Uruguay and, up to a point, Bolivia — resisted authoritarian temptations, many did not entirely. Some muzzled the press, stacked the judiciary, harassed opposition leaders and tampered with electoral systems. Others failed to deal with growing crime and violence.Given the poor state of the economy and the ubiquity of corruption scandals, the left looks likely to continue losing elections: in Brazil, if new ones are held soon; in Ecuador in 2017; and through a recall vote in Venezuela, perhaps later this year. One day, though, leftist parties will win again. When that time comes, the left of tomorrow should learn two lessons from the beginning of this century.First, saving money for bad times is not just a biblical precept. If the left is in power when the next commodity boom comes along, governments need to make provisions for the future. Venezuela and Ecuador should take advantage of higher oil prices — if they come — to create contingency funds under autonomous management. Chile and Peru should do the same with copper.The region’s new middle classes applauded the building projects and health and education programs that were paid for with commodity-boom cash. They deplored the cuts to these efforts, no matter how justified or inevitable they may have been. But left-wing governments should find ways to maintain those programs when revenues shrink. The way to do it is not to pray for more diversified economies — Latin America has never had those and won’t for a long time — but rather to manage resource-based economies more wisely and presciently.But the rainy-day dollars need to be looked after transparently. Which takes us to the second lesson: The causes of corruption across the region — a lack of accountability, a culture of lawlessness, weak institutions and civil society — can affect politicians of the right and left alike. There was no reason to expect that if the old Venezuelan politicians took bribes and traded favors that the new Bolivarian elite would not. Mr. da Silva’s Workers Party came from humble origins in the labor movement, but the fact that it never signed a sweetheart contract when it was in the opposition turned out to be meaningless. Latin American parties should take heed.In the end, the left-wing surge of the early 2000s may have been undone by high expectations as much as anything else. When oil prices fell and Ecuador’s government was no longer able to pay for new roads and more schools, the country’s people got angry because the growth to which they had become accustomed disappeared. When Mr. da Silva was charged with corruption, it showed that he had failed to deliver the change he promised. The leftists came into power with high hopes and big dreams, only to see themselves exposed by their enemies. The best thing to happen to Latin America in recent times has been the clamor for integrity in government; next time around, the left should take up this banner, instead of neglecting it.Jorge G. Castañeda, Mexico’s foreign minister from 2000 to 2003, is a professor at New York University.