There is a narration where a sahabi was given a list of the hypocrites of Madina, so the prophet (pbuh) knew of them but didn't do anything to them. Why did he do this if the punishment of apostasy is death?
Legal punishments are enforced when proven by the required evidence i.e. witnesses or confessions. They are not enforced based solely on knowledge of the ruler. The hypocrites were not executed because the necessary requisites of enforcing the legal punishments were not fulfilled.
The hypocrites did not openly claim to be apostates, rather they outwardly pretended to be Muslims and concealed their disbelief. The Prophet ﷺ was only commanded to enforce outward compliance and to leave the hidden state to Allah.
أمرت أن أقاتل الناس حتى يشهدوا أن لا إله إلا الله وأن محمدا رسول الله، ويقيموا الصلاة، ويؤتوا الزكاة، فإذا فعلوا ذلك عصموا مني دماءهم وأموالهم إلا بحق الإسلام، وحسابهم على الله
I have been ordered (by Allah) to fight against the people until they testify that none has the right to be worshipped but Allah and that Muhammad is Allah's Messenger (ﷺ), and offer the prayers perfectly and give the obligatory charity, so if they perform that, then they save their lives and property from me except for Islamic laws and then their reckoning (accounts) will be done by Allah.
إني لم أومر أن أنقب قلوب الناس
I have not been ordered (by Allah) to search the hearts of the people
And the same applies nowadays. Apostates are only killed when they say or do something which clearly demonstrates their kufr.
The hypocrites would feign repentance when confronted. An apostate who repents and returns to Islam is spared. And according to various madhabs a Zindeeq who feigns repentance is also spared, and he is only killed when he insists on being a disbeliever.
The law of dealing with the hypocrites and other disbelievers changed with time. Initially the Muslims were taught to be lenient as that suited their interests. This is also apparent from the hadith, in which the Prophet ﷺ gave his reason for not executing a hypocrite:
فقام عمر فقال يا رسول الله دعني أضرب عنق هذا المنافق فقال النبي صلى الله عليه وسلم دعه لا يتحدث الناس أن محمدا يقتل أصحابه
Umar got up an, said, "O Allah's Messenger (ﷺ)! Let me chop off the head of this hypocrite (Abdullah bin Ubay)!"
The Prophet (ﷺ) said "Leave him, lest the people say that Muhammad kills his companions."
Which indicates that he abstained from executing him to serve this interest. And that otherwise killing the hypocrite was lawful.
And the Quran also initially prescribed leniency and forgiveness:
ولا تطع الكافرين والمنافقين ودع أذاهم
And do not obey the disbelievers and the hypocrites and disregard their annoyance
فاعفوا واصفحوا حتى يأتي الله بأمره
So pardon and overlook until Allah delivers His command.
However later as the Muslims gained strength, jihad and hudood were prescribed - this means that the hypocrites were punished when their disbelief became evident.
لئن لم ينته المنافقون والذين في قلوبهم مرض والمرجفون في المدينة لنغرينك بهم ثم لا يجاورونك فيها إلا قليلا ملعونين أينما ثقفوا أخذوا وقتلوا تقتيلا
If the hypocrites and those in whose hearts is disease and those who spread rumors in al-Madinah do not cease, We will surely incite you against them; then they will not remain your neighbors therein except for a little. Accursed wherever they are found, [being] seized and massacred completely.
يا أيها النبي جاهد الكفار والمنافقين واغلظ عليهم
O Prophet, fight against the disbelievers and the hypocrites and be harsh upon them.
Hence, if the Prophet ﷺ spared an apostate or proven hypocrite in some isolated incident then that concession was limited to that specific case and to that specific time and has been abrogated. The Muslims now act on the command that has been given to them.
Ref: Tafsir al-Qurtubi