The gospel of John says that the cleansing of the temple was early in Jesus's ministry (John 2:14-21). The other gospels put this event after Jesus enters Jerusalem, near the time he was crucified. So when did it happen?
There are two possible explanations.
One is that the individual Gospel writers did not arrange events in a chronologic order; each one organized the events in a way that made the most sense to their audience or to best fit their theological emphasis.
The second is that Jesus did this on more than one occasion and John records the first which took place near the beginning of His public ministry. Mathew, Mark, and Luke record the second which occurred 2-3 years later.
Besides placing the event at the beginning of the ministry, there are 2 other differences which make John unique. One is in what is missing and the other is in what is found:
Matthew/Mark/Luke: And he was teaching them and saying to them, “Is it not written, ‘My house shall be called a house of prayer for all the nations’? But you have made it a den of robbers.” (Mark 11:17 ESV)
- these I will bring to my holy mountain, and make them joyful in my house of prayer; their burnt offerings and their sacrifices will be accepted on my altar; for my house shall be called a house of prayer for all peoples.” (Isaiah 56:7 ESV)
John: And he told those who sold the pigeons, “Take these things away; do not make my Father's house a house of trade.” His disciples remembered that it was written, “Zeal for your house will consume me.” (John 2:16-17 ESV)
- For zeal for your house has consumed me, and the reproaches of those who reproach you have fallen on me. (Psalm 69:9 ESV)
Matthew, Mark, and Luke all make reference to Jesus saying that His House is to be a House of prayer for all peoples (from Isaiah). They place the reference to what is written with Jesus. The reference is His House being a House of prayer for both the Jewish people and the Gentiles. This is missing from John.
John has something not found in the other 3. John connects clearing the Temple with Psalm 69 which is a prophetic picture of the sacrificial death of Jesus ("the reproaches of those who reproach you have fallen on me"). John also puts the reference to the written Scripture with the disciples as something they later remembered (not something Jesus spoke). The focus in John is different and it excludes the Gentiles.
John then relates the retrospective actions of the disciples:
When therefore he was raised from the dead, his disciples remembered that he had said this, and they believed the Scripture and the word that Jesus had spoken. (John 2:22 ESV)
There is good internal evidence that John is describing something not found in the other 3 accounts, as are most of the events he records.
It also requires one to believe that after this first clearing, Jesus repeated this action when He returned to Jerusalem near the end of His life. The dual clearing of the Temple is consistent with the actual history of the Temple (it was destroyed twice); it also follows the pattern of Jesus returning to earth for a final "clearing."
In answer to the question, when did Jesus cleanse the Temple, I would suggest we consider to two Old Testament contexts, first from Lev 14:34-45 concerning the investigation of corruption in a house (leprous plague) and second from the command in Exo 12:15 to have all leaven removed from your house prior to Passover/Feast of Unleavened Bread.
In regards to the first, we know that part of Jesus' role was to inspect his Father's house - to assess the spiritual state of the leaders because the corruption there had become obvious. We see from Leviticus 14 that there is a process for dealing with corruption in a building construction, which can also be applied to a social construct. There is an initial inspection and clearing out (Lev 14:36) and an evaluation is made, and then the building is quarantined (Lev 14:37-39). If the situation does not correct itself, then the affected parts are removed and replaced (Lev 14:40-42). If the plague returns, then the building is torn down completely (Lev 14:43-45). If Jesus' actions were in fulfillment of this law, then it is possible that he cleared His Father's house initially, but returned to find the corruption was still there. This also lends understanding to Jesus' later prophetic statements that the Temple was to be torn down and a new one raised in its place (Matt 26:61, Matt 27:40) as the corruption remained in it. So it may be possible that the cleansing was not just a one time event, but two events as part of the ongoing inspection of the spiritual state of the leadership of the Temple.
In regard to the passage in John 2:13-21, the Passover reference is significant. Throughout his gospel, John repeatedly makes note when specific events are tied to the feasts of Israel. In regards to Passover and the Feast of Unleavened Bread which are treated as nearly the same feast, there is a command in Exodus 13:7 to remove all leaven from your house prior to the feast, so that "no leavened bread shall be seen among you, nor shall leaven be seen among you in all your quarters." Jesus repeatedly refers to the actions and teachings of the Pharisees as leavening (Matt 16:6, 11, Luke 12:1). So in this instance, paired with the timing of Jesus action, John recognized the significance of Jesus "getting the leaven out" of His Father's house and noted it. It was just another piece of the picture of Passover falling into place.
First of all, there can only have been one cleansing of the Temple. If John is correct, then after the havoc that Jesus caused in the first year of his ministry, the traders and moneychangers would have been aware of Jesus and ready to apprehend him. When Jesus came into the Temple in his final visit to Jerusalem, he would not have been given the opportunity to do this again.
John 2:13 says that when the Passover was at hand, Jesus went to Jerusalem and found sitting in the temple those who sold oxen and sheep and doves, and the money changers. He drove them all out of the temple and poured out the changers' money, and overthrew the tables, saying to take those things and not make his Father's house a marketplace. According to John, Jesus would return four times (John 5:14; 7:14;8:2;10:23) later in his mission apparently without further troubling those he had driven out this first time, nor being troubled by them. So we already have at least some grounds for doubting John's version of this event.
In the synoptic gospels, the cleansing of the Temple was the trigger for the arrest and trial of Jesus:
Mark 11:18: And the scribes and chief priests heard it, and sought how they might destroy him: for they feared him, because all the people was astonished at his doctrine.
However, John says that when Jesus raised Lazarus from the dead, it was the trigger for the arrest and trial of Jesus:
John 11:47-48: Then gathered the chief priests and the Pharisees a council, and said, What do we? for this man doeth many miracles. If we let him thus alone, all men will believe on him: and the Romans shall come and take away both our place and nation.
It would have been a clumsy narrative if the author of John's Gospel had left the now redundant cleansing of the Temple where he had found it in the synoptic gospels, but at the same time it appears he wanted to keep this dramatic narrative, so moved it to the beginning of Jesus' mission.
Nearly all New Testament scholars accept Mark to have been a source for Luke - John Dominic Crossan, in The Birth of Christianity, page 111, calls this a massive consensus - and there is now a growing consensus for Luke to have been a major source for John. Crossan does a technical analysis (ibid, page 565) to show that the author of John was also aware of Mark's Gospel. Andrew Lincoln (Gospel According to St John: Black's New Testament Commentaries, page 29), traces the development of the consensus of John's dependence on the synoptics. He says that there are clear signs of John's dependence on the synoptics in the resurrection narratives, the passion narratives and elsewhere in the gospel, which can not be satisfactorily explained by other means. Once again, if John was dependent on the synoptics, then any discrepancy must be resolved in favour of the synoptics.
This still leaves open the question of whether there was a cleansing of the temple at all. Robert M. Price says in The Christ Myth Theory and Its Problems, page 310, that Mark seems to have been unaware of the vast size of the temple and the fact that any such action would have brought the temple guards down on Jesus' head then and there. I think he may have been aware of these problems but chose to play them down, whereas John at least gives a hint of the scale of things when it mentions the oxen and sheep that had to be driven from the temple courtyard. Price says (ibid, page 125) for Jesus to commandeer it like this would have required a military raid - he mentions the example of Simon bar Gioras' entry into the temple on the eve of its destruction.