# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: gravity-forms, privacy, cleanup
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/gf-delete-old-entries/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Delete Old Gravity Forms Entries Automatically" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
