How to Automatically Delete Submissions Older Than X Days

Published in Convert Forms
Updated 05 Nov, 2020
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Would you like to automatically delete old submissions that are older than a specific amount of days? This is quite useful when you don't want to keep old submissions and would like to automatically remove them instead of manually going over them. The following PHP snippet will allow you to automatically delete old submissions.

Setup

To delete your old submissions, copy the code shown below and place it into the PHP Scripts -> After Form Submission area of your form.

// Set how long the submissions need to be in order to be deleted
$total_days_ago = 5;

// Do not edit below
$form_id = $submission->form_id;

$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__convertforms_conversions'))
->where($db->quoteName('form_id') . ' = ' . $form_id)
->where('DATE(created) = DATE_SUB(CURDATE(), INTERVAL ' . $total_days_ago . ' DAY)');

$db->setQuery($query);

$rows = $db->loadObjectList();

foreach ($rows as $key => $row)
{
      ConvertForms\Api::removeSubmission($row->id);
}

Remember to set how long your submissions needs to be in order to be removed by setting the variable $total_days_ago