Share via


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

ErrorCode158_Error

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. "

ErrorCode158_ErrorDelete

So now it made sense, all you need to do is switch the MasterPage in the Site Settings.

ErrorCode158_SiteSettings

ErrorCode158_SetMasterPage

Done!

Comments