Skip links
WordPress Trash

How to Automatically Empty Your WordPress Trash

Would you like the garbage on your WordPress website to be emptied automatically? By default, WordPress deletes anything that has been in the trash for 30 days. However, some users may prefer the garbage to be emptied more or less frequently.

In this post, we’ll teach you how to empty your trash automatically on a daily, weekly, or any other schedule you want.

What Is Trash in WordPress?

WordPress included the garbage feature in version 2.9. It functions similarly to your computer’s recycle bin or trash. In other words, WordPress relocates your deleted posts to the trash instead of immediately deleting them. This allows you to recover them before WordPress permanently deletes them after a set amount of time.

Trash in WordPress

Anyone can click the ‘Delete’ link on a post by accident. That is why WordPress allows you to easily restore deleted posts and pages. Your WordPress website will automatically delete anything in the trash after 30 days so that it does not accumulate and keep using server resources.

If you want to prevent WordPress from emptying the trash automatically, see our guide on how to disable automatic trash emptying in WordPress. However, what if you like to have the garbage emptied more frequently than once a month?

Let’s have a look at how to automatically empty the trash in WordPress after a set amount of time. We will look at two techniques. Use the links below to go to your favourite method:

Method 1: Automatically Empty WordPress Trash Using WPD

In this way, we will use a custom code snippet to automatically empty the trash in WordPress. You can include this code snippet in your WordPress theme’s functions.php file. However, even minor coding errors can cause your website to fail and become unreachable.

This is why we recommend using WPCode. It is the best code snippets plugin for WordPress, allowing you to add custom code to your site without disrupting it. First, install and activate the free WPCode plugin. For more information, please visit our article on how to install WordPress plugins.

Once activated, navigate to the Code Snippets » +Add Snippet page from the WordPress admin sidebar.

Click the ‘Use Snippet‘ button below the ‘Add Your Custom Code (New Snippet)‘ option.

Add Snippet

This will take you to the ‘Create Custom Snippet‘ page, where you can begin by providing a title for your code snippet. It could be anything that allows you to recognize it.

After that, pick ‘PHP Snippet‘ as the ‘Code Type‘ from the selection panel on the right.

PHP Snippet

Next, 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 the code as is, the system will empty your trash every 24 hours.

However, you can easily change the number 1 in the code to a different number, adjusting the trash to be emptied after that many days.

For example, by using 15 instead of 1 in the code, as shown below, WordPress will remove your trash every 15 days:

define('EMPTY_TRASH_DAYS', 15 ); //Integer is the amount of days
Integer is the amount of days

If you set the number of days to zero, the WordPress trash option will be deleted completely. WordPress will then permanently erase the posts and pages without putting them in the trash.

Next, scroll down to the insertion area and select ‘Auto Insert‘ mode.

When you save the snippet, the custom code will become live on your site.

Auto Insert

Scroll to the top of the page and turn the ‘Inactive‘ switch to ‘Active.’

Finally, remember to click the ‘Save Snippet‘ button to save your changes and use the code on your website.

Save Snippet

Method 2: Automatically Empty WordPress Trash With a Plugin

If you don’t want to use code on your website, this is the method for you. First, install and activate the Change Empty Trash Time plugin. For additional information, visit our step-by-step tutorial on how to install a WordPress plugin.

Once activated, navigate to the Settings » General tab in the WordPress admin area. Once there, scroll to the bottom of the screen to discover the new ‘Time to Empty Trash‘ option.

Simply enter the number of days that WordPress should take to empty the garbage. For example, you can enter 1 for daily, 7 for weekly, and 30 for monthly. If you input 0, the system will permanently erase the removed posts and pages instead of placing them in the trash. Be very careful when setting the time to empty to zero.

It may be impossible to restore permanently deleted posts and pages. Make sure to use a backup plugin just in case. Once you finish, click the ‘Save Changes‘ button to save your settings.

Also, Read More Relevant Articles

The Final Line

Finally, setting up automated garbage disposal in WordPress is a simple but efficient technique to keep your website running smoothly and organized. By automating this procedure, you may reduce clutter, conserve database space, and ensure that your site functions well without requiring continual manual maintenance. This minor change can significantly impact.

Leave a comment

This website uses cookies to improve your web experience.
Home
Account
Cart
Search
Explore
Drag