Delete content

When deleting a content item, you must delete its master version. This way the every other version of the item will be deleted too. To delete an item, you must perform the following:

  1. Get the master version.
    Get the master version of the content item.
  2. Delete the master version.
    Delete the master version. All other lifecycle versions will be deleted too.

This topic explains how to delete a content item by its ID.

You can perform these actions by using either Native API or Fluent API. Therefore, the sections below provide code snippets for both APIs.

NOTE: In the following examples, news items will be used as content. The code can also be applied to blog posts, list items, events, images, documents, and videos.

Delete an item by the ID of its master version

The following code deletes an item by the ID of its master version.

Native API

using System;
using System.Linq;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
namespace SitefinityWebApp
{
public class DeleteContent_DeleteItemByMasterIDNativeAPI
{
private void DeleteItemByMasterIDNativeAPI(Guid masterId)
{
NewsManager manager = NewsManager.GetManager();
//Get the master version of the item.
NewsItem master = manager.GetNewsItems().Where(item => item.Id == masterId).SingleOrDefault();
if (master != null)
{
//Mark the item to be deleted.
manager.Delete(master);
//Save the changes.
manager.SaveChanges();
}
}
}
}

First, you get an instance of the manager. Then, you get the master version with the specified ID. Then, you mark the item to be deleted by calling the Delete method of the manager with the master version as an argument. Finally, you save the changes.

Fluent API

using System;
using Telerik.Sitefinity;
namespace SitefinityWebApp
{
public class DeleteContent_DeleteItemByMasterIDFluentAPI
{
private void DeleteItemByMasterIDFluentAPI(Guid masterId)
{
App.WorkWith().NewsItem(masterId).Delete().SaveChanges();
}
}
}

First, you get the singular facade of the master version with the specified ID. Then, you call the Delete method to mark the item to be deleted. Finally, you call SaveChanges.

Delete an item by the ID of its live version

The following code deletes an item by the ID of its live version.

Native API

using System;
using System.Linq;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
namespace SitefinityWebApp
{
public class DeleteContent_DeleteItemByLiveIDNativeAPI
{
private void DeleteItemByLiveIDNativeAPI(Guid liveId)
{
NewsManager manager = NewsManager.GetManager();
//Get the live version of the item.
NewsItem live = manager.GetNewsItems().Where(item => item.Id == liveId).SingleOrDefault();
if (live != null)
{
//Get the master version.
NewsItem master = manager.Lifecycle.GetMaster(live) as NewsItem;
//Mark the item to be deleted.
manager.Delete(master);
//Save the changes.
manager.SaveChanges();
}
}
}
}

First, you get an instance of the manager. Then, you get the live version with the specified ID. Then, you get the master version by calling Lifecycle.GetMaster with the live version as an argument. Then, you mark the item to be deleted by calling the Delete method of the manager with the master version as an argument. Finally, you save the changes.

Fluent API

using System;
using System.Linq;
using Telerik.Sitefinity;
using Telerik.Sitefinity.News.Model;
namespace SitefinityWebApp
{
public class DeleteContent_DeleteItemByLiveIDFluentAPI
{
private void DeleteItemByLiveIDFluentAPI(Guid liveId)
{
//Get the live version.
NewsItem live = App.WorkWith().NewsItems().Where(item => item.Id == liveId).Get().SingleOrDefault();
if (live != null)
{
//Get the Id of the master version.
Guid masterId = App.WorkWith().NewsItem().GetManager().GetMaster(live).Id;
//Delete the item.
App.WorkWith().NewsItem(masterId).Delete().SaveChanges();
}
}
}
}

First, you get the instance of the live version with the specified ID. Then, you get the ID of the master version by using the manager that is exposed by the singular facade. The you get the singular facade for the master version. Then, to mark the item to be deleted, you call the Delete method. Finally, you call SaveChanges.

Delete all items

The following code deletes all of the items.

Native API

using System.Linq;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
namespace SitefinityWebApp
{
public class DeleteContent_DeleteAllItemsNativeAPI
{
private void DeleteAllItemsNativeAPI()
{
NewsManager manager = NewsManager.GetManager();
var items = manager.GetNewsItems().Where(item => item.Status == ContentLifecycleStatus.Master).ToList();
foreach (NewsItem item in items)
{
manager.Delete(item);
}
manager.SaveChanges();
}
}
}

First, you get an instance of the manager. Then, you get the master versions of the available items. You iterate through the collection and mark each item to be deleted. Finally, you save the changes.

Fluent API

using Telerik.Sitefinity;
namespace SitefinityWebApp
{
public class DeleteContent_DeleteAllItemsFluentAPI
{
private void DeleteAllItemsFluentAPI()
{
App.WorkWith().NewsItems().Delete().SaveChanges();
}
}
}

First, you get the plural facade of the content item. Then, to mark each item to be deleted, you call the Delete method. Finally, you save the changes.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?