Automatically Clear Your WordPress Trash: An Easy Guide
Introduction
Is your WordPress trash causing clutter on your website? WordPress automatically clears the trash after 30 days, but what if you want to clear it more frequently or less often? In this article, we will show you two methods to automatically empty your WordPress trash, so you can maintain a clutter-free website.
Method 1: Using a Code Snippet
If you want to automate the process of emptying your WordPress trash, you can start by using a code snippet. However, it is important to be cautious when adding custom code to your site. We recommend using the WPCode plugin, which allows you to safely incorporate custom code onto your site.
- Install and activate the free WPCode plugin on your WordPress site.
- Access the ‘Code Snippets’ page in your WordPress admin sidebar and click on ‘+Add Snippet’.
- Enter a title for your code snippet for future reference.
- Select ‘PHP Snippet’ as the code type.
- Copy and paste the provided code snippet below:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'wp_schedule_event' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
function wpb_automatically_empty_trash() {
wp_scheduled_delete();
}
add_action( 'wp_scheduled_delete', 'wpb_automatically_empty_trash' );
- By default, this code snippet will empty your trash every 24 hours. If you want to change the interval, replace the ‘1’ with your desired number of days. For example, if you want your trash to be emptied every 15 days, change ‘1’ to ’15’.
- Scroll down and select ‘Auto Insert’ mode for the insertion section.
- Toggle the switch from ‘Inactive’ to ‘Active’ at the top of the page.
- Click on ‘Save Snippet’ to store your settings and activate the code on your site.
Before activating this feature, it is essential to back up your WordPress site to avoid accidentally deleting important posts or pages.
Method 2: Using a Plugin
If you prefer not to add code directly to your website, you can use a WordPress plugin to automatically empty your trash.
- Install and activate the ‘Change Empty Trash Time’ plugin on your WordPress site.
- Navigate to ‘Settings’ > ‘General’ in your WordPress admin area.
- Scroll down to the ‘Time to empty trash’ option located at the bottom of the page.
- Set the number of days you want WordPress to wait before emptying the trash. For daily emptying, enter ‘1’. For weekly, enter ‘7’. To keep deleted posts and pages permanently, enter ‘0’.
- Be cautious when setting the time to empty to ‘0’, as this will result in permanent deletion of content. To ensure additional protection, consider using a backup plugin such as Duplicator.
- Click on ‘Save Changes’ to store your preferences.
That’s it! Your WordPress trash will now be automatically emptied based on your desired interval. Keeping your trash clean will optimize your website’s performance and improve the user experience.
We hope this easy guide has been helpful in assisting you in automating the process of emptying your trash in WordPress.