It was all a leela between Parvati,Siva and **Vishnu** in order to give Ganesa a primary place in worship, because as created, he was said to be of Tamasika nature:

https://www.wisdomlib.org/hinduism/book/shiva-purana-english/d/doc226136.html

>1. O Nārada, on hearing your words, the great lord who grants benediction to his devotees became desirous of fighting with the boy.

>2. He called Viṣṇu and consulted him. Then with a great army and the gods, He, the three-eyed lord, stood face to face with him.

>3. After remembering the lotuslike feet of Śiva, the gods possessing great strength, kindly glanced at by Śiva and highly jubilant, fought with him.

>4. Viṣṇu of great strength, valour and skill and possessing great divine weapons and Śivā’s form fought with him.

>5. Gaṇeśa hit all the chief gods with his staff. He hit Viṣṇu too, all of a sudden. The hero had been conferred great strength by the Śaktis.

>6. O sage, all the gods including Viṣṇu were hit by him with the stick. They were turned back with their strength sapped.

>7. O sage, after fighting for a long time along with the army and seeing him terrific, even Śiva was greatly surprised.

>8. Thinking within himself “He has to be killed only by deception and not otherwise” he stayed in the midst of the army.

>9-10. When lord Śiva who though devoid of attributes had assumed the attributive form was seen in the battle, when Viṣṇu too had come thither, the gods and Gaṇas of Śiva were highly delighted. They joined together and became jubilant.

>11. Then Gaṇeśa the heroic son of Śakti following the course of heroes, at first worshipped (i.e struck) Viṣṇu with his staff, Viṣṇu who confers happiness to all.

>12-13. “I shall cause him delusion. Then let him be killed by you, O lord. Without deception he cannot be killed. **He is of Tāmasika nature** and inaccessible.” Thinking thus and consulting Śiva, Viṣṇu secured Śiva’s permission and was engaged in the activities of delusion.

Vishnu had a strong role in the actual killing of Ganesa:

>25-27. On seeing Gaṇeśa, Viṣṇu said—“He is blessed. He is a great hero of great strength. He is valorous and fond of battle. Many gods, Danavas, Daityas, Yakṣas, Gandharvas, and Rakṣasas I have seen. In the entire extent of the three worlds, none of them can equal Gaṇeśa in regard to brilliance, form, features, valour and other qualities.”

>28. Gaṇeśa, son of the Śaktis whirled the iron club and hurled it at Viṣṇu even as he was saying so.

>29. After remembering the lotus-like feet of Śiva, Viṣṇu took up his discus and split the iron club by means of discus.

>30. Gaṇeśa hurled the piece of iron club at Viṣṇu which was caught by the bird Garuḍa and rendered futile.

>31. Thus for a long time the two heroes Viṣṇu and Gaṇeśa fought with each other.

>32. Again the foremost among heroes, the son of Pārvatī took up his staff of unrivalled power remembering Śiva and struck Viṣṇu with it.

>33. Struck with that unbearable blow he fell on the ground. But he got up, quickly and fought with Pārvatī’s son.

>34. Securing this opportunity, the Trident-bearing deity came there and cut off his head with his trident.

The Gods were worried Parvati might destroy creation and prayed to her:

>32. O Mother of the universe, obeisance to you. Obeisance to you, O Śivā. Obeisance to you. O Caṇḍikā. Obeisance to you, Kalyāṇī.

>33. O mother, you alone are the primordial Śakti. You are the eternal cause of creation. You alone are the sustaining power. You alone are the cause of dissolution.

>34. O goddess, be pleased. Spread peace. Obeisance be to you. O goddess, the three worlds are agitated by your fury.

>Brahmā said:—
35. The great goddess Pārvatī thus eulogised by you and other sages glanced at them furiously. She did not say anything.

>36. Then the sages bowed at her lotus like feet and spoke to her in low voice with devotion joining their palms in reverence.

>The sages said:—
37. O goddess, forgive, forgive. The final dissolution seems near at hand. Your lord is standing here. O mother, you see him.

>38. What are we, the gods, Viṣṇu, Brahmā and others? We are only your subjects. We stand here with palms joined in reverence.

>39. O great goddess, our guilts shall be forgiven. We are agitated and distressed. O Pārvatī give us peace.

>Brahmā said:—
40. After saying this, the agitated and distressed sages stood in front of her with palms joined in reverence.

>41. On hearing their words Pārvatī was pleased and she replied to the sages with her mind full of compassion.

>The goddess said:—
42-43. If my son regains life there may not be further annihilation. If you can arrange for him an honourable status and position among you as the chief presiding officer, there may be peace in the world. Otherwise you will never be happy.

Siva often kills temporarily (like Daksha) and this was all a leela to remove tamas from Ganesa and make him worthy of worship.

Parvati tells the purified Ganesa

>You are blessed and contented now. **You will receive worship before all the gods.** You will be free from distress.

Another example of a play-fight is Siva fighting Arjuna in the Kiratarjuneeyam fight only to purify Arjuna's body:

Mahabharata Critical Edition page 1195:

>Tryambaka touched the immensely energetic Phalguna and everything that was impure in his body, was immediately destroyed.