I'm afraid elephants simply roll over the stakes as if they weren't there. In fact, not only that, they actually crush the stakes so that they will no longer harm cavalry. Thus the AI can use them to clear a path if you have blocked off a gateway/river crossing.