Delete Old Gravity Forms Entries Automatically
Deletes Gravity Forms entries older than the number of days you choose once a day, so you do not keep personal data longer than you need.
Opens WPIDE on
Code
$wpide_gf_keep_days = 0; // Days to keep entries, e.g. 180 (0 = off until you set it).
$wpide_gf_purge_forms = array(); // Form IDs; empty = all forms.
add_action( 'init', function () {
if ( ! wp_next_scheduled( 'wpide_gf_purge_entries' ) ) {
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'wpide_gf_purge_entries' );
}
} );
add_action( 'wpide_gf_purge_entries', function () use ( $wpide_gf_keep_days, $wpide_gf_purge_forms ) {
if ( ! class_exists( 'GFAPI' ) || $wpide_gf_keep_days < 1 ) {
return;
}
$ids = GFAPI::get_entry_ids(
$wpide_gf_purge_forms ? array_map( 'intval', $wpide_gf_purge_forms ) : 0,
array( 'end_date' => wp_date( 'Y-m-d H:i:s', time() - $wpide_gf_keep_days * DAY_IN_SECONDS ) ),
null,
array( 'offset' => 0, 'page_size' => 100 )
);
foreach ( $ids as $id ) {
GFAPI::delete_entry( $id );
}
} );
Notes
Does nothing until you set the number of days at the top; it then deletes old entries permanently (including uploaded files) in batches of 100 per daily run, so export anything you need first.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.