I read through Luther’s collected works one summer about 23 years ago (I still remember the Library of Congress call number off the top of my head because I had to go back and forth to pick up 50 or so volumes a few at a time: BR 330.E5 1955).
Luther believed in Purgatory until the late 1520s. He essentially emptied it of meaning as time went by. He finally fully discarded it because it no longer could fit into his invented system.
Luther believed in Purgatory because he was raised in the Roman Catholic religion and he was a doctor of theology. Over time, he discarded many of the false doctrines he had been taught that were invented in Catholicism because he saw that they contradicted God's holy word.