The US invasion of Mexico has also been characterized as the first US "foreign" war, but it was not. By 1846, the United States had invaded, occupied, and ethnically cleansed dozens of foreign nations east of the Mississippi. Then there were the Barbary Wars. The opening lyric of the official hymn of the US Marine Corps, composed and adopted soon after the invasion of Mexico, "From the Halls of Montezuma to the shores of Tripoli," refers in part to 1809–5, when the marines were dispatched by President Thomas Jefferson to invade the Berber Nation of North Africa. This was the "First Barbary Wary," the ostensible goal of which was to persuade Tripoli to release US sailors it held hostage and to end "pirate" attacks on US merchant ships. The "Second Barbary War," in 1815–16, ended when pasha Yusuf Karamanli, ruler of Tripoli, agreed not to exact fees from US ships entering their territorial waters.
— An Indigenous Peoples' History of the United States (ReVisioning American History) by Roxanne Dunbar-Ortiz, Roxanne Dunbar Ortiz (Page 119)