An unexpected danger can arise even while someone is engaged in prayer. In this incident the Prophet ﷺ struck the scorpion that had stung him, showing that a real threat may call for appropriate protective action.
This event must not be turned into blanket permission to harm animals. There is an important distinction between causing needless harm and protecting oneself from an actual threat. The account does not encourage killing harmless creatures out of dislike.
Detailed legal rulings about actions during prayer belong to a more advanced discussion. The introductory lesson is that genuine personal safety is not ignored.