Do you want the trash on your WordPress website to be emptied automatically?
By default, WordPress automatically deletes anything that has been in the trash for 30 days. But some users might like the trash to be emptied more or less often.
In this article, we’ll 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’ll 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’ll 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.
Upon activation, you need to visit the Code Snippets » +Add Snippet page from the admin sidebar.
This will take you to the ‘Add Snippet’ screen where you’ll see some ready-to-use code snippets.
Since we need to add custom code, you need to take your mouse over to the ‘Add Your Custom Code (New Snippet)’ option and then click the ‘Use snippet’ button.
Once, you’re on the ‘Create Custom Snippet’ page, you can start by typing a title for your code snippet. It can be anything that will help you identify it.
After that, you need to select ‘PHP Snippet’ as the ‘Code Type’ from the dropdown menu on the right.
Next, you need to simply copy and paste the following code in 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, you need to scroll down to the insertion section and choose ‘Auto Insert’ as the insert method. The custom code will now 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 the 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.
Upon activation, you need to visit the Settings » General page from the admin sidebar.
Once there, scroll down to the bottom of the page where you will find a new ‘Time to empty trash’ option.
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’re using a backup plugin just in case. We recommend using Duplicator for this.
Once you’re done, don’t forget to click the ‘Save Changes’ button to store your settings.
We hope this tutorial helped you learn how to automatically empty your trash in WordPress. You may also want to learn how to get a free SSL certificate or check out the list of must-have WordPress plugins to grow your site.
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.
john moses says
This is not clear at allmaybe for professionals only
kenal says
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 says
awesome i got tired to trash topic and comments every day thanks for the awesome solution