First question is what you mean by a phalanx?
I will be assuming you mean the phalanx as fielded by Alexander the Great: A large formation of trained pikemen, presenting a seemingly inpenetrable wall of spearpoints to the enemy as they advanced like an unstoppable force of nature. This is the formation described by Polybius as being virtually unbeatable in a frontal assault, after all.
Phalanx is also used to describe the earlier formations of heavily armed hoplites in classical Greece, but those earlier phalanxes were nothing special, both in their armaments (spears and shields) and in their training (they had none.) and they could be beaten by pretty much anything, circumstances permitting.
So, we'll be talking about the Hellenistic pike-phalanx.
Second question is: what do you mean by destroyed?
A less obvious question than you might think. Ancient battles (and medieval, and pre-modern) were not like video games. Armies generally were not "destroyed" as in killed. Battles could be horrific and bloody affairs, but they were frequently short and usually did not result in the deaths of that many of their participants. Peter Krentz did some estimates of casualties in pre-Hellenistic hoplite battles (using a rather inadequate dataset, it must be said. He did the best he could but we simply do not have nearly enough data about these battles to speak with great confidence.) and came to something like 5% casualties for the victors and 14% for the loser in Greek battles. In Roman battles the rates appear to be even more lop-sided. The implication is, and this is confirmed by narrative sources, that most people died AFTER their side lost the battle and they were trying to run away. The victors would pursue them and kill them piecemeal as they fled.
Of course, making the enemy flee is easier said then done when they're trained professionals with a strong esprit de corps, as many phalangites were. Morale is very hard to predict, and one of the most common situations for troops to flee is when they (believe they) are losing the battle, so fighting them still needed to be done. So let's shelve this point for a moment and look at what our sources had to say about the phalanx.
Polybios famously describes the sheer density of a phalanx formation and compares it to the Romans:
Many considerations may easily convince us that, if only the phalanx has its proper formation and strength, nothing can resist it face to face or withstand its charge. For as a man in close order of battle occupies a space of three feet; and as the length of the sarissae is sixteen cubits according to the original design, which has been reduced in practice to fourteen; and as of these fourteen four must be deducted, to allow for the distance between the two hands holding it, and to balance the weight in front; it follows clearly that each hoplite will have ten cubits of his sarissae projecting beyond his body, when he lowers it with both hands, as he advances against the enemy: hence, too, though the men of the second, third, and fourth rank will have their sarissae projecting farther beyond the front rank than the men of the fifth, yet even these last will have two cubits of their sarissae beyond the front rank; if only the phalanx is properly formed and the men close up properly both flank and rear
[...]
The result of this will be that each Roman soldier will face two of the front rank of a phalanx, so that he has to encounter and fight against ten spears, which one man cannot find time even to cut away, when once the two lines are engaged, nor force his way through easily.
(Emphasis mine)
A strong case in favour of the phalanx. Yet Polybios wrote his book to explain how the Romans managed to conquer all the Hellenistic kingdoms.
Polybios gives his explanations which I'll quote below, but I'll stress one of my own: He's probably wrong here, or at least being far too theoretical and mechanistic. Phalanxes, we know from other sources, fought in various different densitities depending on circumstances. And whilst they COULD form an extremely densely packed block of pike-points like this, this also made it pretty much impossible to manoeuvre. It was only really useful to mount a stationary defense against a cavalry charge. Typically they had a much less dense formation that allowed them to move without all falling down if one guy tripped.
But even then it is clear that in a frontal assault such a formation was very difficult to fight in hand-to-hand combat, as long as they maintained proper order.
Now consider for a moment the conditionals in that sentence.
Polybios already mentioned these things two thousand years ago:
And what is it that brings disaster on those who employ the phalanx? Why, just because war is full of uncertainties both as to time and place; whereas there is but one time and one kind of ground in which a phalanx can fully work. If, then, there were anything to compel the enemy to accommodate himself to the time and place of the phalanx, when about to fight a general engagement, it would be but natural to expect that those who employed the phalanx would always carry off the victory. But if the enemy finds it possible, and even easy, to avoid its attack, what becomes of its formidable character?
Makes sense, right? But keep in mind my earlier point: Polybios is being too theoretical here, and although these weaknesses were real, the phalanx was more flexible than it appears from this.
Still, that's not the most important issue. The more important issue is that I think this is only a part of the story. Like I mentioned at the start, battles were very rarely won because one side had longer pointy sticks than the other. Battles were won because one side was more motivated and fought more bravely, or because one side was better fed and the other army suffered disease, or because something caused the other side to panic .(the fearsome reputation of the other army? The death of a general? The rumour of the death of a general? Enemies appearing from an unexpected direction? A sudden bad omen?) The Greeks were very much aware of the unpredictability of war, and of men in war.
Battles could be won and lost for any reason. Even if the phalanx had not had the weaknesses described above, it still woud have lost battles, simply because it consisted of human beings who could and did make mistakes.
A number of ways.
To begin with, a phalanx is composed of people. Those people need food and water. If you can cut off the phalanx from their supply, it can compel them to retreat. This was actually the case at the Battle of Plataea when the Persians were confronting the Greeks. Herodotus writes:
"The herald having thus spoken waited for some time, and then, as no one made him any answer, he departed and went back; and having returned he signified to Mardonios that which had happened to him. Mardonios then being greatly rejoiced and elated by his empty victory, sent the cavalry to attack the Hellenes: and when the horsemen had ridden to attack them, they did damage to the whole army of the Hellenes by hurling javelins against them and shooting with bows, being mounted archers and hard therefore to fight against: and they disturbed and choked up the spring Gargaphia, from which the whole army of the Hellenes was drawing its water. Now the Lacedemonians alone were posted near this spring, and it was at some distance from the rest of the Hellenes, according as they chanced to be posted, while the Asopos was near at hand; but when they were kept away from the Asopos, then they used to go backwards and forwards to this spring; for they were not permitted by the horsemen and archers to fetch water from the river.."
Source: https://www.gutenberg.org/files/2456/2456-h/2456-h.htm
The result of this was that the Greek commanders called a conference and decided they had to withdraw. Of course, the sequence of events led to a closely-fought battle which resulted in a Greek victory, but it illustrates how there was more to achieving a victory over an organized body of infantry than just defeating them in melee.
Alternatively, if the opposing phalanx was lacking in missile support, one could use light troops to defeat them. This occurred at the Battle of Lechaeum, where Athenian skirmishers badly mauled Spartan hoplites:
"The Lacedaemonians were presently within range of the javelins. Here a man was wounded, and there another dropped, not to rise again. Each time orders were given to the attendant shield-bearers to pick up the men and bear them into Lechaeum; and these indeed were the only members of the mora who were, strictly speaking, saved. Then the polemarch ordered the ten-years-service men to charge and drive off their assailants. Charge, however, as they might, they took nothing by their pains--not a man could they come at within javelin range. Being heavy infantry opposed to light troops, before they could get to close quarters the enemy's word of command sounded "Retire!" whilst as soon as their own ranks fell back, scattered as they were in consequence of a charge where each man's individual speed had told, Iphicrates and his men turned right about and renewed the javelin attack, while others, running alongside, harassed their exposed flank. At the very first charge the assailants had shot down nine or ten, and, encouraged by this success, pressed on with increasing audacity. These attacks told so severely that the polemarch a second time gave the order (and this time for the fifteen-years-service men) to charge. The order was promptly obeyed, but on retiring they lost more men than on the first occasion, and it was not until the pick and flower of the division had succumbed that they were joined by their returning cavalry, in whose company they once again attempted a charge. The light infantry gave way, but the attack of the cavalry was feebly enforced. Instead of pressing home the charge until at least they had sabred some of the enemy, they kept their horses abreast of their infantry skirmishers, charging and wheeling side by side."
Source: https://en.wikisource.org/wiki/Hellenica_(Xenophon)/Book_4/Chapter_5#5:11
Missile cavalry could also be highly effective (with the repeated caveat that the opposing phalanx be without sufficient ranged support). A phalanx is generally just a group of heavy infantry in close order (they do not have to be hoplites), and Roman legionnaires proved quite vulnerable to Parthian horse archers if they were isolated:
"But the Parthians now stood at long intervals from one another and began to shoot their arrows from all sides at once, not with any accurate aim (for the dense formation of the Romans would not suffer an archer to miss even if he wished it), but making vigorous and powerful shots from bows which were large and mighty and curved so as to discharge their missiles with great force. At once, then, the plight of the Romans was a grievous one; for if they kept their ranks, they were wounded in great numbers, and if they tried to come to close quarters with the enemy, they were just as far from effecting anything and suffered just as much. For the Parthians shot as they fled, and next to the Scythians, they do this most effectively; and it is a very clever thing to seek safety while still fighting, and to take away the shame of flight."
Source: http://penelope.uchicago.edu/Thayer/E/Roman/Texts/Plutarch/Lives/Crassus*.html
Of course, those incidents were situations that favored such approaches. Battles were rarely scenarios where only one type of warrior was fielded. Phalanxes were often defeated through mistakes made by their commanders, rather than by a perfect set of tactics being discovered and used.