Auto Delete Form entries

By: Uttam N | Asked: 12/22/2023
ForumsCategory: How-toAuto Delete Form entries
Uttam N asked 11 months ago
Hi Team, Can anyone help here, How I can achieve the  auto delete functionality of old entries. I had gone through docs but didn't find relevant. e.g: I am looking scheduler which will triggered daily and will delete  previous(last)  day all records.   Thanks.
2 Answers
Rob LeVineRob LeVine answered 11 months ago
Create a PHP program on your server that limits the entries to what you want to delete (keying on the created_at value) and iterate over that list calling FrmEntry::destroy on each entry. Then create a CRON job to call the PHP program.
Uttam N replied 11 months ago

Thanks for reply, I had implemented the script and tried to run file placing in wordpress under 'public_html/sampleDeleteEntries.php' folder. I think its not connecting to Formidable Entries. Can you please verify, if I am missing or doing anything wrong.

Below is my script file 'sampleDeleteEntries.php'

$form_id,
'start_date' => $start_date,
));

// Check if entries were found
if ($entries) {
// Loop through entries and delete them
foreach ($entries as $entry) {
//FrmEntry::destroy($entry->id, true); // true parameter for permanent deletion
$deleted_count++;

// Print the deleted entry ID
echo "Deleted Entry ID: {$entry->id}\n";
}

// Print success message with total count
echo "Successfully deleted {$deleted_count} entries for Form ID {$form_id}.\n";
} else {
// Print message if no entries found
echo "No entries found for deletion.\n";
}
}

// Set the Form ID
$form_id = 5; // Replace with Form ID

// Calculate start date for entries older than 24 hours
$start_date = date('Y-m-d H:i:s', strtotime('-24 hours')); // Go back 24 hours

// Delete entries and print messages
delete_entries($form_id, $start_date);

?>

Rob LeVineRob LeVine replied 11 months ago

A few things. 1. The beginning of your code paste is incomplete, so it's not clear what the full code is. 2. What do you mean by " I think its not connecting to Formidable Entries.", i.e., what messages are you receiving on the front end or in a log file? 3. What is the code for "delete_entries"? 4. I suggest using some sort of online code pasting website for sharing code here because the formatting is pretty much non-existent. 5. You may be missing the following code to load up WordPress in your standalone program:
$path = preg_replace('/wp-content.*$/','',__DIR__);
require($path . '/wp-load.php');

Uttam N replied 11 months ago

Thanks for reply. After adding Library Path. Script able to execute with below fetch 2 records.

$entries = FrmEntry::getAll(array('it.form_id' => $form_id), ' ORDER BY it.created_at DESC',2);

But in my case, I need to fetch/query entries till specific date range, for that I had constructed below query, But its not working even though old records are present in Entries. Anything I am missing in constructing query please guide me.

$entries = FrmEntry::getAll(array('form_id' => $form_id,'where' => array('created_at $start_date, ), ));

Where $start_date format is 'Y-m-d H:i:s' and value getting passed is '2023-12-24 10:27:12'.

Rob LeVineRob LeVine replied 11 months ago

That command isn't syntactically correct because you don't have an operator to associate the column name and the value. I'm surprised you didn't get an error. It's potentially better to do something like this, using a direct SQL command as seen here - https://pastebin.com/35yTek9E . NOTE: I didn't test this.

Uttam N answered 11 months ago
Thanks, I am replying from Answer box as  after posting comment the code/query getting changed. Below is query I am trying to execute, to fetch Entries till specific datetime(start_date). Also tried the link, but I think its broken.
$entries = FrmEntry::getAll(array('form_id' => $form_id, 'where' => array( 'created_at <= %s' => $start_date), ));

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right