Clear Sitecore 9 Forms data

Sitecore 9 has added forms feature and we do not need an external module anymore. While we all have been using this, I came across a requirement of deleting all forms data when we completed testing it.

This is a very common scenario, but currently there is no native feature to do so. I contacted Sitecore support for this and they informed that they will be adding this feature in coming version. But till then, we can clear forms data and it is pretty simple.

Sitecore manages all the data in instanceName_ExperienceForms database. It has two tables –

  • FormsEntry
    • It records all entries submitted for forms along with FormId
  • FieldData
    • It records all entered data values for all form fields along with FormEntryID from above table.

You can clear this data to clear form data. Here is a database script that you can use to delete data of

  • all forms
  • specific form
  • form in a date range

Just make sure you execute it on your instanceName_ExperienceForms database.

BEGIN TRANSACTION [ClearFormData]
  Declare @createdFrom DateTime
  Declare @createdTo DateTime
  Declare @formId uniqueidentifier 
  Set @createdFrom = '2019-05-20' -- set your from date here (Range include both these dates)
  Set @createdTo = '2019-05-21' -- set your to date here
  Set @formId = '3317432C-DF54-4E54-B92A-1BA9AAEA2294' -- form id in same format as this (remove '{}')

  BEGIN TRY

	Delete FieldData
	from FormEntry right join [FieldData]
	on [FormEntry].ID = [FieldData].[FormEntryID]
--	where FormItemID = @formId -- * Uncomment this line to delete data for a single form *
--	and (cast(Created as date) between @createdFrom and @createdTo) -- * Uncomment this line to delete for mentioned duration *

      Delete From [FormEntry]  
--	  where FormItemID = @formId -- * Uncomment this line to delete data for a single form *
--	and (cast(Created as date) between @createdFrom and @createdTo) -- * Uncomment this line to delete for mentioned duration *

      COMMIT TRANSACTION [ClearFormData]
  END TRY
  BEGIN CATCH
      ROLLBACK TRANSACTION [ClearFormData]
  END CATCH

Happy Coding !!