The US most certainly funded the afghan war against the soviets to hurt the soviets, and hurting the soviets was all about stopping the spread of communism. You're looking at this too narrowly, some would call it a proxy war, it was yet one more hot war in the cold war. They used to call it the domino theory. And it worked, because the Soviet Union did fall. And it fell because it's economy couldn't keep growing with all those resources going to the soviets wars of expansion.
But thanks for reminding me, another example: the US liberated Afghanistan from the Taliban.