"World's policeman", that's what you tell little kids America was doing. America didn't invade Iraq or Afghanistan for world peace. There were strong economic and strategic motives behind those invasions.
Strategic motivation? If one assumes the US is going to be globally involved, yes, but that's begging the question.
Economic motivation? Not so much now, with the US being a dominant oil producer, and with petroleum itself losing importance. Even then, it's questionable if this could justify the full cost of the US military.
I think the original motivation was two fold: it was a combination of some sort of moral obligation to defend the "free world" from authoritarians, and (after WW2) a desire to keep small countries (and recent WW2 enemies) from deciding their only option for defense was their own nuclear deterrent.
Another thing the US did in the post-war world was apply economic leverage to dismantle European empires. This can also be viewed as defending nations against external coercion.
I think it had something to do with 9/11 being an act of war from Afghanistan against the US. Nations are responsible for the actions of groups inside their borders against other nations.
It was not an act of war since Afghanistan didn't have an official government - in practice the Taliban ran things - but the attacks were carried out by the Al Qaeda which was spread over the Middle East. The Taliban might have been sympathetic to it but they were not actively supporting them or had any official collaboration with them.
I hope you see where the problem with this is - the US had an enemy in a supranational organization, the Al Qaeda, which resided in many countries including Afghanistan.
The government of said country was unfriendly but not actively hostile to the US and on good terms with AQ, but not outright allies. This could've been said to apply between many Middle Eastern governments and radical groups at the time.
The US decided to invade, and antagonized the formerly unfriendly Taliban to become actively hostile.
The US managed to temporarily win over the Taliban but failed to permanently displace them.
AQ leadership, including Bin Laden moved out of the country almost immediately.
The 'war on terror' went on almost without end, then Bin Laden was killed a decade later, in a different country the US didn't declare war on, thanks to US special force action.
While AQ got weaker, ISIS got stronger (honestly I don't follow ME insurgent groups that closely, I wouldn't be surprised if this was a rebrand/reorganization in part).
So the US-initiated invasion totally failed to reach its stated result while leaving a huge collateral in its wake.
At the same time, soft power is also vanishing.