I've just tried the following code on fresh installation of Kentico 12 MVC and it worked well. I've added this class to CMSApp CMS project, into CMSModules folder:
using CMS.DataEngine;
using CMS.EventLog;
using CMS.OnlineForms;
using CMS.Scheduler;
using CMS.SiteProvider;
namespace CMSApp.CMSModules
{
public class CustomTask : ITask
{
public string Execute(TaskInfo ti)
{
var details = "Custom scheduled task executed. Task data: " + ti.TaskData;
EventLogProvider.LogInformation("CustomTask", "Execute", details);
var formObject = BizFormInfoProvider.GetBizFormInfo("DancingGoatMvcContactUsNew", SiteContext.CurrentSiteID);
if (formObject == null) return null;
var formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
var formClassName = formClass.ClassName;
var data = BizFormItemProvider.GetItems(formClassName)
.WhereEmpty("TestText");
foreach (var item in data)
{
BizFormInfoProvider.DeleteBizFormRecordFiles(formClass.ClassFormDefinition, item, SiteContext.CurrentSiteName);
item.Delete();
}
return null;
}
}
}
This is how the task was configured in Kentico (registering custom class is not mandatory, you can just select ITask implementation from the assembly you are using):
In the code I was using DancingGoatMvcContactUsNew as form code name:
And this is the data before running the task:
This is after (you can see that all the records with empty Test text were removed):
The only other reason I can think of is that you may have some custom module that uses some Form events and cancels the delete operation or something, but that would have been strange..