A deceased person may be taken out of the grave for a legitimate Shari’ah reason.
(Albani, may Allah have mercy on him) holds this view.
➊ It is narrated from Jabir (may Allah be pleased with him) that the Messenger of Allah ﷺ came when Abdullah ibn Ubayy (the chief of the hypocrites) had already been placed in his grave:
فامر به فأخرج
“But by the command of the Prophet ﷺ he was taken out of the grave.”
Then the Prophet ﷺ placed him on his knees, put some of his saliva into his mouth, and clothed him in his shirt. Now Allah alone knows best (probably the reason for such treatment of the hypocrite after his death was that) he had clothed Abbas (may Allah be pleased with him) in a shirt.
➋ It is narrated from Jabir (may Allah be pleased with him) that another Companion was buried in the grave with my father, but my heart was not at ease with that, so I took out his body and buried him in another grave.
(Shawkani, may Allah have mercy on him) holds this action to be permissible.