Privacy PHP Intermediate

Delete Old Completed Privacy Requests

Automatically delete export and erasure requests 30 days after they're completed, along with their email addresses.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 19 lines
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 );
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • If you need proof that a request was handled, keep your own record, such as a note in your support system, before they’re deleted.
  • Change '30 days ago' to keep them longer.
  • If you remove the snippet, also clear the event with wp_clear_scheduled_hook( 'nahnu_delete_old_privacy_requests' );.

How to test it

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.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only