Matthew 21:12 And Jesus went into the temple of God, and cast out all them that sold and bought in the temple, and overthrew the tables of the moneychangers, and the seats of them that sold doves, 13 And said unto them, It is written, My house shall be called the house of prayer; but ye have made it a den of thieves.
John 2:12 After this he went down to Capernaum, he, and his mother, and his brethren, and his disciples: and they continued there not many days. 13 And the Jews' passover was at hand, and Jesus went up to Jerusalem, 14 And found in the temple those that sold oxen and sheep and doves, and the changers of money sitting: 15 And when he had made a scourge of small cords, he drove them all out of the temple, and the sheep, and the oxen; and poured out the changers' money, and overthrew the tables; 16 And said unto them that sold doves, Take these things hence; make not my Father's house an house of merchandise. 17 And his disciples remembered that it was written, The zeal of thine house hath eaten me up.
This was done during the Passover celebration in Jerusalem of the exodus that took place every year. The greed of the merchants would come to the Temple to sell animals for the sacrifices as they would jack up their prices for the doves and or pigeons and even the cattle and sheep as it was hard for some to travel such distances with these sacrifices for Passover feast.
The money changers would transfer Roman coins to Jewish coins in order for those Jews who traveled to pay a half a shekel as a tribute to the service of the sanctuary according to Jewish law, Exodus 30:11-16. The money exchanger's would charge a small sum for exchanging Roman coins to Jewish coins.
In the first cleansing it was the Jews that immediately confronted Jesus in the Temple, John 2:18, but in the second cleansing it was the chief priest and scribes who confronted Him.
Matthew 21:17-32 The second time the Temple was cleansed was about three years later when Jesus left Bethany where He lodged there that night and in the morning made His triumphant entry into Jerusalem during the last week of His life. This time Jesus did not make a scourge of small cords to drive them out the chief priest and scribes, but condemned them instead.
Jesus went into the Temple to teach and the chief priest and scribes confronted Jesus demanding by what authority and who gave Him that authority to speak as He did. Jesus ended up telling them that publicans and harlots will go into the kingdom of God before they ever would because they believed John the Baptist and repented, but they believed not and repented not.