How to Limit or Disable Automatic Empty Trash in WordPress

In WordPress, when delete a post or comment, it goes into trash. By default, trashed posts and comments remain in your database for 30 days giving you enough time to restore them if needed. After 30 days, WordPress automatically deletes all trash content permanently. In this article, we will show you how to limit or disable automatic WordPress empty trash feature.

What is Trash in WordPress?

When you delete a post, page, or a comment in WordPress, it is marked as trash.
trashinwpposts
You can see the trashed posts or comments by clicking on the Trash link when viewing posts, pages, or comments.
By default, these items will remain there for 30 days. After that, WordPress will automatically delete them permanently.
You can override the automatic process by going to trash and deleting the items manually by clicking on the delete permanently link.
deletetrash
But what if you don’t want WordPress to delete items from trash? Or maybe you want WordPress to automatically delete items sooner or later than 30 days.
Let’s take a look at how to limit or disable automatic WordPress empty trash feature.

Stopping WordPress from Automatically Emptying Trash

Do you want to stop WordPress from automatically deleting items from trash? Here is what you need to do.
Simply add this little code snippet in your theme’s functions.php file or a site-specific plugin.

function wpb_remove_schedule_delete() {
    remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
}
add_action( 'init', 'wpb_remove_schedule_delete' );

This code simply removes the action that deletes trashed items when their time is up.
Now when you send an item to trash, it will remain there until you go to trash and manually empty the trash.

Changing When to Empty Trash in WordPress

As we mentioned earlier, WordPress automatically empty trash after 30 days. You can change this to any number of days you want.
Simply add the following line of code in your wp-config.php file just before the line that says ‘That’s all, stop editing! Happy blogging.’

define('EMPTY_TRASH_DAYS', 7);

This line changes the trash emptying schedule to 7 days. You can change 7 to any number of days that you want.

Disabling The Trash Feature in WordPress

We don’t recommend disabling the trash functionality in WordPress. It is a great feature that comes in handy when you accidentally delete a post, page, or a comment.
However, if you really feel that you don’t need this functionality, and you would rather delete things permanently, then here is what you need to do.
Simply add this line of code in your wp-config.php file just before the line that says ‘That’s all, stop editing! Happy blogging.’

define('EMPTY_TRASH_DAYS', 0);

Adding this line will disable the WordPress trash feature completely. On your posts and pages, custom post types, and comments pages, you will see Delete Permanently link instead of Trash.
trashdisabled
That’s all, we hope this article helped you learn how to limit or disable automatic empty trash feature in WordPress.



Leave a Reply