While developing a custom feature receiver, I was attempting to delete a masterpage file from the MasterPage Gallery when I received an SPException with the message: "Cannot remove file "<filename>". Error Code 158" Since I found no help on the web, I thought I would share this simple solution. I did the usual routine of checking if the file was checked out or missing, or if the user didn't have permissions. But then I tested whether or not I could delete it in the UI, and I received this message: "You cannot delete the master page. It is currently set as the default master page for the web site."
So now it made sense, all you need to do is switch the MasterPage in the Site Settings. Done!
PingBack from http://blog.a-foton.ru/2008/08/cannot-remove-file-error-code-158/