Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.