Delete comments

Sitefinity CMS allows you to delete a single comment or a group of comments through the Comments API.

Delete a single comment by its key

When deleting a comment, you must perform the following:

  1. Get an instance of the comments service by using the SystemManager's GetCommentsService method.
  2. Delete the comment using the DeleteComment method of the comments service. The method expects the comment key as a parameter.

Consider the code sample below:


Delete multiple comments by their keys

When deleting multiple comments, follow the steps below:

  1. Get an instance of the comments service using the SystemManager's GetCommentsService method.
  2. Delete all comments using the DeleteComments method of the comments service. The methods expects a collection of comment keys as a parameter.

Consider the code sample below:



Delete all comments for a specific content type

In case you  need to delete programmatically all comments for a specific content type, for example News, follow the steps below:

  1. Get an instance of the comments service.
    Use the GetCommentsService method of the SystemManager.
  2. To find all comments related to News, you use the CommentFilter and add NewsItem full type name as a criteria.
    Thus, you can use the GetComments method and pass the filter as an argument. For more information, see Query comments.
  3. Delete comments.
    After retrieving all keys of the comments, you can use the DeleteComments method. 

Consider the code sample below:



Delete all comments for a specific content item by the item's ID

You may need to programmatically delete all comments for a specific content item by the item's ID. In the following example, you delete all comments for a specific News item by its ID. Follow the steps below:

  1. Get an instance of the comments service.
    Use the GetCommentsService method of the SystemManager.
  2. To find all comments related to the specific News item, you use the ID of the News item and create a new thread key.
    Add the thread key in the CommentFilter and use the filter the get all comments. For more information, see Query comments.

Consider the code sample below:

Was this article helpful?

Next article

Work with threads