Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Automatically Empty Your WordPress Trash

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.

How to automatically empty your WordPress trash

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.

Trash link

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:

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.

Add new snippet

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.

Choose PHP as the code type

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
Change the number to your liking in the code

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.

Choose an insertion method

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.

Save the snippet for emptying trash in WordPress

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.

Use a plugin to empty trash in WordPress

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.

Duplicator Plugin Mascot created by Solomon Balkhi

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.

Customize your WordPress backup

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.

View scan of your backup

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

5 CommentsLeave a Reply

  1. 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

  2. john moses

    This is not clear at allmaybe for professionals only

  3. 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.

  4. Ahmad

    awesome i got tired to trash topic and comments every day thanks for the awesome solution

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.