Delete pages

Sitefinity CMS allows you to unpublish and then delete  the PageNode and the underlying PageData through the code.

To unpublish and delete a page:

  1. Get the page node or data.

    When deleting the page, first, you must get the page node or data. For more information about finding a specific page node and data, see For developers: Query pages.

  2. Unpublish the page.

    It's a good practice, to set the ApprovalWorkflowState to Unpublished before doing the deletion.

    The following example uses the Native API to unpublish the page with the specified Title:

    C#
    using System.Linq;
    using Telerik.Sitefinity.Modules.Pages;
    
    namespace SitefinityWebApp
    {
        public class DeletePages_UnpublishPageNativeAPI
        {
            public void DeletePages_UnpublishPage_NativeAPI()
            {
                PageManager pageManager = PageManager.GetManager();
                var page = pageManager.GetPageNodes().Where(pn => pn.Title == "Test").FirstOrDefault();
                page.ApprovalWorkflowState = "Unpublished";
                pageManager.UnpublishPage(page.GetPageData());
                pageManager.SaveChanges();
            }
        }
    }
  3. Delete the page.

    After you get and unpublish the page, you delete it.

  4. Save the changes.

    Finally, you must save the changes.

To delete a page you can use the Native API or the Fluent API.

To delete a page using the Native API, you must use the PageManager class. For more information, see Delete a page using Native API.

To delete a page using the Fluent API, you must use the page facade. For more information, see Delete a page using Fluent API.

Delete a page using Native API

To delete a specific page with the Native API, you use the PageManager class and LINQ queries.

The following example deletes the page with the specified Title:

C#
using System.Linq;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;

namespace SitefinityWebApp
{
    public class DeletePages_DeletePageNativeAPI
    {
        public void DeletePageNativeAPI(string pageTitleToDelete)
        {
            PageManager pageManager = PageManager.GetManager();
            PageData page = pageManager.GetPageDataList().Where(pD => (pD.NavigationNode.Title == pageTitleToDelete && pD.Status == ContentLifecycleStatus.Live)).FirstOrDefault();

            if (page != null)
            {
                pageManager.Delete(page);
                pageManager.SaveChanges();
            }
        }
    }
}

Delete a page using Fluent API

The following example deletes the page with the specified Title:

C#
using Telerik.Sitefinity;

namespace SitefinityWebApp
{
    public class DeletePages_DeletePageFluentAPI
    {
        public void DeletePageFluentAPI(string pageTitleToDelete)
        {
            App.WorkWith().Pages()
                .Where(pN => pN.Title == pageTitleToDelete)
                .Delete()
                .SaveChanges();
        }
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.