Make WordPress Core

Opened 19 months ago

Last modified 19 months ago

#58470 new enhancement

Add configurable batching to `wp_scheduled_delete` for controlling cleanup operations

Reported by: johnrom's profile johnrom Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.3
Component: Cron API Keywords:
Focuses: performance Cc:

Description

When posts are trashed, they are automatically cleaned up by the wp_scheduled_delete wp-cron task. However, both the frequency that this cron task runs and the number of posts that can be cleaned up at once time lack configurability.

Imagine a shop which has more than 100,000 orders: trashing thousands or tens of thousands of them at once could cause the website to become unresponsive when it comes time to clean up the trash.

I suggest adding two constants:

Constant Name Default Value Description
EMPTY_TRASH_SCHEDULE daily The cron schedule on which the trash scheduler runs. This is useful for increasing the number of times the cleanup process runs when using batching to delete many posts.
EMPTY_TRASH_BATCH_SIZE 50 The maximum number of posts to delete at once.

It may also be useful for these and EMPTY_TRASH_DAYS to have filters to improve cleanup operations overall.

Change History (2)

#1 @peterwilsoncc
19 months ago

The frequency of the task can be modified via the schedule_event filter using code similar to that below (untested but you'll get the idea if it needs fixing).

<?php
add_filter( 'schedule_event', function( $event ) {
        if ( 'wp_scheduled_delete' !== $event->hook ) {
                return $event;
        }
        
        $event->schedule = 'twicedaily'; /* or hourly/custom schedule name */
        
        
        return $event;
} );

Allowing plugin authors to modify the batch size seems like it could be helpful but rather than using a constant (WordPress aims to avoid them when possible) I think a filter in wp_scheduled_delete would be a better approach.

#2 @johnrom
19 months ago

I agree a filter is better, but I suggested a constant since there is already precedent for configuring this area with a constant.

It seems a little odd to filter on something so generic rather than to use a filter which expresses intent like wp_scheduled_delete_schedule, but as long as it's possible I can live with it.

Note: See TracTickets for help on using tickets.