Cannot remove file "". Error Code 158

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.




Skip to main content