For context: We occupied Japan for, what, 12 years? We technically occupied Germany longer, but practically it was about the same (I think - from memory and not researched). In that time, we transformed them from violent, racially superior, conquer-the-world militarists to more-or-less western-values democracies. But we put a lot of people in there, and we controlled every aspect of their society.
Iraq and Afghanistan, we tried to do it with half-measures rather than a complete rebuild. And it failed.
One started because we won a decisive war against the nation, in the other we invaded the country on a flimsy excuse.
We have entirely different cultures, times, mores, media landscapes, and different series of occupations, modern weaponry, 4th generation warefare (post vietnam) tactics, the list goes on.
Why would "oh we didn't waste enough money occupying a country that we gained little from" be the right choice? How do we magically know that?