Remove post revisions older than 90 days with a daily scheduled task, keeping the database lean without losing recent history.
add_action( 'init', function (): void {
if ( ! wp_next_scheduled( 'nahnu_delete_old_revisions' ) ) {
wp_schedule_event( time(), 'daily', 'nahnu_delete_old_revisions' );
}
} );
add_action( 'nahnu_delete_old_revisions', function (): void {
$old = get_posts( [
'post_type' => 'revision',
'post_status' => 'inherit',
'posts_per_page' => 200,
'fields' => 'ids',
'no_found_rows' => true,
'date_query' => [ [ 'column' => 'post_modified_gmt', 'before' => '90 days ago' ] ],
] );
foreach ( $old as $revision_id ) {
wp_delete_post_revision( $revision_id );
}
} );
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 time you save a post, WordPress stores a full copy as a revision. On busy sites that adds up to tens of thousands of rows. Limiting revisions stops new ones piling up; this snippet clears out old ones.
Once a day, a scheduled task finds up to 200 revisions last modified more than 90 days ago and deletes them with wp_delete_post_revision(), which also removes their meta. Working in batches keeps each run quick; a big backlog clears over a few days.
Revisions from the last 90 days stay, so you can still undo recent changes.
Run the task straight away with WP Crontrol, or wp cron event run nahnu_delete_old_revisions, then check an old post: revisions older than 90 days should be gone.