The problem is that we can’t stay forever, and we can’t eradicate the bad people. Eventually we will leave, and there will still be bad people. Unless we plan to reduce the country to a pile of rubble and start over, we can’t win a war of occupation, and we should never have tried.
I imagine Obama recognized that and spent a lot of effort on finding a way out. It’s not surprising he didn’t do it in his first term, because being the guy who lost both wars might have cost him the election. He really missed an opportunity, though, by not withdrawing during his second term.