Automatically delete export and erasure requests 30 days after they're completed, along with their email addresses.
add_action( 'init', function (): void {
if ( ! wp_next_scheduled( 'nahnu_delete_old_privacy_requests' ) ) {
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'nahnu_delete_old_privacy_requests' );
}
} );
add_action( 'nahnu_delete_old_privacy_requests', function (): void {
$old = get_posts( [
'post_type' => 'user_request',
'post_status' => [ 'request-completed', 'request-failed' ],
'date_query' => [ [ 'column' => 'post_modified_gmt', 'before' => '30 days ago' ] ],
'fields' => 'ids',
'posts_per_page' => 100,
] );
foreach ( $old as $request_id ) {
wp_delete_post( $request_id, true );
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Every personal data export or erasure request is kept forever, including the requester’s email address. Keeping it a short while is useful as a record. Keeping it for years isn’t. This snippet clears completed requests after 30 days.
Privacy requests are stored as posts of type user_request. A daily WP-Cron event finds completed or failed requests that haven’t changed in 30 days and deletes them permanently, 100 at a time.
Pending and confirmed requests are never touched.
'30 days ago' to keep them longer.wp_clear_scheduled_hook( 'nahnu_delete_old_privacy_requests' );.Mark a test request as completed, then change its modified date in the database to 31 days ago. Run wp cron event run nahnu_delete_old_privacy_requests. It should disappear from Tools → Export Personal Data.