Halloween Sale 15% off Ends in See the plans
PHP

Remove Completed Privacy Requests After 90 Days

Deletes finished export and erasure requests (and failed ones) from Tools > Export/Erase Personal Data once they are 90 days old, since the request records themselves contain email addresses.

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

Code

// Age in days after which finished requests are removed.
$wpide_request_days = 90;

add_action( 'init', function () {
    if ( ! wp_next_scheduled( 'wpide_clean_privacy_requests' ) ) {
        wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'wpide_clean_privacy_requests' );
    }
} );

add_action( 'wpide_clean_privacy_requests', function () use ( $wpide_request_days ) {
    $ids = get_posts( array(
        'post_type'      => 'user_request',
        'post_status'    => array( 'request-completed', 'request-failed' ),
        'posts_per_page' => 100,
        'fields'         => 'ids',
        'no_found_rows'  => true,
        'date_query'     => array( array( 'column' => 'post_modified_gmt', 'before' => $wpide_request_days . ' days ago' ) ),
    ) );

    foreach ( $ids as $id ) {
        wp_delete_post( $id, true );
    }
} );

Notes

Keep records longer if you need them as proof that requests were handled.

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.