Requirement:
After deploying a new master page as part of new branding, We wanted to delete the old master page from all SharePoint sites. As we've applied the new master page, we don't want our existing custom master page to be appear in the master page gallery and master page selection page.
Solution:
PowerShell script to delete the SharePoint master page.
Remember, If the master page you are trying to delete is configured as either Default master page or custom master page, You can't delete it! change the master page in your code first and then try deleting it again.
Related post: Change Master Page in SharePoint using PowerShell
After deploying a new master page as part of new branding, We wanted to delete the old master page from all SharePoint sites. As we've applied the new master page, we don't want our existing custom master page to be appear in the master page gallery and master page selection page.
Solution:
PowerShell script to delete the SharePoint master page.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinueThis PowerShell script just deletes given master page from the given web. Loop through each web and delete the master page from all.
#Site URL and Master page Name variables
$WebURL="https://portal.crescent.com/sites/sales/"
$MasterPageName="crescent.master"
#Get Objects
$web = Get-SPWeb $WebURL
$MasterPage = $web.GetFolder("_catalogs/masterpage").Files[$MasterPageName]
#Delete the Master page using PowerShell
$MasterPage.Delete();
write-host "Master page deleted!"
Remember, If the master page you are trying to delete is configured as either Default master page or custom master page, You can't delete it! change the master page in your code first and then try deleting it again.
Related post: Change Master Page in SharePoint using PowerShell