By default, WordPress automatically deletes anything that has been in the trash for 30 days. While this feature is useful, allowing your trash to accumulate can take up valuable server space and potentially slow down your site.
In our experience, we’ve found that some users may benefit from their trash being emptied more often. This is especially true for sites that publish and update a lot of content.
In this article, we will show you how to automatically empty your trash daily, weekly, or at any interval you want.
What Is Trash in WordPress?
WordPress added a trash feature way back in version 2.9. It works just like the recycle bin or trash on your computer.
In other words, your deleted posts aren’t removed immediately but are sent to the trash instead. So, they can be recovered before WordPress deletes them forever after a certain period of time.
Anyone can click the ‘Delete’ link on a post accidentally. That’s why WordPress makes it easy to restore deleted posts and pages.
Your WordPress website will automatically delete anything in the trash after 30 days so that it doesn’t pile up and continue to use server resources.
If you’d like to stop WordPress from emptying the trash automatically, then you can follow our guide on how to disable automatic empty trash in WordPress.
However, what if you prefer the trash to be emptied more regularly than just once a month?
Let’s see how you can automatically empty trash in WordPress after a certain time period.
We will cover two methods. You can use the links below to jump to your preferred method:
Method 1: Automatically Empty WordPress Trash With WPCode (Recommended)
For this method, we will be using a custom code snippet to automatically empty trash in WordPress.
You can add this code snippet to your WordPress theme’s functions.php file. However, a tiny error in the code can break your website and make it inaccessible.
That’s why we recommend using WPCode. It is the best code snippets plugin for WordPress that allows you to safely add custom code to your website without breaking it.
First, you’ll need to install and activate the free WPCode plugin. For more details, please see our guide on how to install a WordPress plugin.
Note: WPCode also has a paid plan that you can install to unlock amazing features like a code snippet library, conditional logic, create a custom block, and more.
Upon activation, visit the Code Snippets » +Add Snippet page from the WordPress admin sidebar.
From here, click the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.
This will direct you to the ‘Create Custom Snippet’ page, where you can start by typing a title for your code snippet. It can be anything that will help you identify it.
After that, select the ‘PHP Snippet’ option as the ‘Code Type’ from the dropdown menu on the right.
Next, simply copy and paste the following code into the ‘Code Preview’ box:
define('EMPTY_TRASH_DAYS', 1 ); //Integer is the amount of days
If you leave this code as it is, then your trash will be emptied every 24 hours.
However, you can simply change the number 1 in the code to some other number so that the trash will be emptied for that number of days.
For instance, you can write 15 instead of 1 in the code, as in the example below, and your WordPress trash will be emptied every 15 days:
define('EMPTY_TRASH_DAYS', 15 ); //Integer is the amount of days
If you set the number of days to 0, however, the WordPress trash feature will be removed entirely. WordPress will then permanently delete posts and pages without placing them in the trash.
Note: Make sure to have a backup of your WordPress website before activating this feature so that you can restore any accidentally deleted pages or posts.
Next, scroll down to the insertion section and choose ‘Auto Insert’ mode.
The custom code will be live on your site whenever you save the snippet.
After that, scroll to the top of the page and toggle the ‘Inactive’ switch to ‘Active.’
Finally, don’t forget to click on the ‘Save Snippet’ button to store your settings and execute the code on your website.
Method 2: Automatically Empty WordPress Trash With a Plugin
If you don’t want to add code to your website, then this method is for you.
First, you’ll need to install and activate the Change Empty Trash Time plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Dislaimer: Keep in mind that the plugin hasn’t been tested with the latest versions of WordPress. However, we used the plugin on our website and it seems to work fine. For details, see our guide on the use of outdated plugins.
Upon activation, head over to the Settings » General page from the WordPress admin area.
Once there, scroll down to the bottom of the page, where you will find a new ‘Time to empty trash’ option.
Here, simply type in the number of days that WordPress should take to empty the trash. For example, you can type 1 for daily, 7 for weekly, or 30 for monthly.
If you type 0, then the deleted posts and pages will be deleted permanently instead of being placed in the trash.
Note: Be very careful about setting the time to empty to 0. It can be difficult to recover permanently deleted posts and pages. Make sure you are using a backup plugin, just in case.
Once you are done, don’t forget to click the ‘Save Changes’ button to store your settings.
Bonus: Create Your Website Backups
If you have removed the trash option and want to delete content from your site permanently, then we recommend creating a backup for your website first.
To do this, you can use Duplicator, which is the best WordPress backup plugin on the market. It is super beginner-friendly and has amazing features like scheduled backups, recovery points, cloud storage integration, migration tools, and more.
Upon plugin activation, visit the Duplicator Pro » Packages page and click the ‘Create New’ button in the top corner.
This will take you to a new screen where you must add a name for the backup that you are creating. After that, select a storage location and scroll down to the Backup section to customize your backup.
For instance, the plugin allows you to include or exclude different parts like the database, core, plugins, media, etc.
Next, you have to fill in the additional settings in the installer and continue forward.
Duplicator will now perform a scan and make sure everything is in order to create a backup. Once that’s done, you can click the ‘Create Backup’ button.
You have now successfully created a site backup and can restore any content that you deleted accidentally. For detailed instructions, see our beginner’s guide on how to back up your WordPress site.
We hope this tutorial helped you learn how to automatically empty your trash in WordPress. You may also want to learn how to manage multiple WordPress sites from one dashboard or check out the list of plugins and tips to improve the admin area.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Abdolaziz
When I try to insert this code, WPCode shows this error: “Constant EMPTY_TRASH_DAYS already defined”
WPBeginner Support
That would normally be an issue if the code is not being triggered at the correct time. Please ensure it’s set to auto-insert and it should resolve the issue. If not if you reach out to WPCode’s support and they can look into the issue further
Admin
john moses
This is not clear at allmaybe for professionals only
kenal
I found this very useful because even after deleting a post, slide or comment. Going to the trash to permanently empty it is always the next step. Makes me more lazy in regards to that which is cool.
Ahmad
awesome i got tired to trash topic and comments every day thanks for the awesome solution