The first thing to note about this is that the law chartering the Second Bank of the United States required the secretary of the treasury to deposit federal tax revenue in the B.U.S. Jackson’s removal was thus in one sense illegal.
On the other hand, as Jackson made clear in his Bank Bill Veto Message (1832), he did not grant the validity of the Supreme Court’s decision in _McCulloch v. Maryland_ (1819) beyond the case at hand. In other words, although McCulloch won, Jackson denied that the Court’s decision bound him. Since he held that the B.U.S.’s charter was unconstitutional, he did not think he had to follow the law granting that charter.
In general, Jackson was prone to do what personal animus drove him to do. Thus, for example, he came down hard on South Carolina in the Nullification Crisis even though neighboring Georgia was simultaneously nullifying the Supreme Court’s pronouncements concerning the rights of Indians in Georgia. Why? Because Martin Van Buren had just disclosed to him that Vice President John C. Calhoun of South Carolina had as secretary of war in 1817 called for then-General Jackson to be relieved of his command. At least, that’s how I read it.
Essentially, Jackson turned to the pet banks because he had to do something with the federal tax revenue, and he hadn’t thought out the problem before he removed the deposits. For a fuller account of this imbroglio, see:
Kevin R. C. Gutzman, “Andrew Jackson’s War Against the Second Bank of the United States,” in _Conflicts in American History, Volume II: The Early Republic Era, 1783-1860_, ed. C. Brid Nicholson (Bruccoli Clark Layman, 2010), 212-36.