In Umbraco 7, I used ContentService
API to insert, update or delete documents. After inserting or updating a document, the correct content shows immediately, but after deleting, removed data can be viewed because of the cache. To clear cache, I delete DistCache
folder's data from App_data
.
How can I refresh the cache programmatically?
Here is my code:
public string DeleteStudent(int studentID)
{
IContentService service = ApplicationContext.Current.Services.ContentService;
if (studentID != 0)
{
IContent student = service.GetById(studentID);
if (student != null && student.ContentType.Alias == "dtStudent")
{
service.Delete(student);
return "Done!";
}
}
return "Error!";
}
As @Harvey said in the comment, using
service.MoveToRecycleBin
works fine. That method will unpublish the content, then move it to the recycle bin.
Final code:
public string DeleteStudent(int studentID)
{
IContentService service = ApplicationContext.Current.Services.ContentService;
if (studentID != 0)
{
IContent student = service.GetById(studentID);
if (student != null && student.ContentType.Alias == "dtStudent")
{
service.MoveToRecycleBin(student);
return "Done!";
}
}
return "Error!";
}