All content of that list will be deleted. Use at your own risk!
What it does:
First a template of that list is being created. After that the list is deleted completely. Than a new list using that template is being created and at the end that template is being deleted.
Works damn fast (just a few seconds even on big lists). IF anything goes wrong you can use the template to create a list manually.
public void TruncateList(Guid listID)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = SPContext.Current.Web)
{
try
{
web.AllowUnsafeUpdates = true;
// alte Werte merken
SPList listCopy = web.Lists\[listID\];
string title = listCopy.Title;
string description = listCopy.Description;
string backupName = string.Format("BACKUP{0}{1:d} {1:HH}{1:mm}", title,DateTime.Now);
// save template
listCopy.SaveAsTemplate(backupName, backupName, string.Empty, false);
SPListTemplate template = web.Site.GetCustomListTemplates(web)\[backupName\];
// delete list
listCopy.Delete();
web.Lists.Add(title, description, template);
// remove template
SPList gallery = web.Lists\["List Template Gallery"\];
foreach (SPListItem item in gallery.Items.Cast().Where(item => item.Title == backupName))
{
item.Delete();
break;
}
web.Update();
web.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
\_log.Error(ex);
throw;
}
}
}
}
);
}