When we want to delete a particular layout from layout gallery and it is being referred by pages (pages created by this layout), we will not be able to delete the layout. It will redirect us to error page stating-"This item cannot be deleted because it is still referenced by other pages." So, to remove the layout, we will have to remove all the pages which are referring the layout and then we will be able to remove the layout. Alternatively, we can move the layout to a temporary folder by opening the master page gallery in window explorer and then remove the temporary folder. Moving a layout to a different folder will break all the page references and therefore removing the layout will not create any problem.
This we can also do through code as below-
private static void DeleteLayoutFromGallery(SPWeb web, string layoutName)
SPFile layout = web.GetFile(web.Site.Url.ToString() + "/_catalogs/masterpage/" + layoutName);
SPFolder layoutFolder = layout.ParentFolder;
SPFolder tempFolder = layoutFolder.SubFolders.Add("tempFolder");
layout.MoveTo(tempFolder.Url + "/" + layout.Name);
The important point to note is- All the pages refering the deleted layout will not get the layout reference and hence will get corrupted. Even if we re-add those layouts through feature, broken references will still be broken and pages will remain be corrupted. If we will try to open those pages, It will display error message reading- "Resource can not be found."