The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Automatically Empty WordPress Trash After 2 Days

Shared by Amit Dholiya · October 17, 2026 · @empty_trash_days

26 views
1 upvote
Back to Snippets

Steps

  1. Add the PHP code to your child theme’s functions.php file or the Code Snippets plugin.

  2. Save and activate the snippet.

  3. WordPress will use a seven-day trash retention period when its scheduled cleanup runs.

Hook Used

– empty_trash_days — Sets how many days WordPress retains trashed content before automatic cleanup.

Type: WordPress Maintenance

Output: Trashed content becomes eligible for automatic permanent deletion after seven days.

Customize: Change return 2; to another number, such as 7.

Warning: Permanently deleted content cannot normally be restored from WordPress Trash. Keep regular backups before enabling automatic cleanup.

function custom_set_trash_retention_period( $days ) {
    return 2;
}
add_filter( 'empty_trash_days', 'custom_set_trash_retention_period' );

Tested on

WordPress 6.8

Reported by the author on October 17, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.4. It may still work.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by