delete trash automatically wordpress

How to Automatically Delete Trash from WordPress

Do you want to Automatically Delete Trash from WordPress? In WordPress, When You delete a post or comment, it goes into the trash.

By default, trashed posts and comments remain in your database for 30 days giving you enough time to restore them if needed.

wordpress trash arestore

After 30 days, WordPress automatically deletes all trash Posts, pages and Comments permanently. In this article, we will show you, How to empty trash sooner than 30 days and how you can Disable Automatic Empty Trash in WordPress.

Automatically Delete Trash from WordPress

With the help of wp-config, you can configure how frequently WordPress empties your trash. You can specify the number of days after which your post, pages, attachments you moved to the Trash folder will be emptied.

If you use 0 as value the Trash feature will be completely disabled on your site. You need to add the following line of code to the beginning of your wp-config.php file:

define( 'EMPTY_TRASH_DAYS', 2 );

In above code 2 represents the number of days after trash will be removed automatically as you probably guessed you can change the number of days according to your necessities.

As I mentioned earlier If you use 0 as value the Trash feature will be completely disabled on your website. Meaning Trash will be cleared as soon as you hit the trash button so, keep that in mind.

Disable Automatic Empty Trash in WordPress

In order to disable automatically empty trash feature, you need to add following code to your theme’s functions.php file or you can code snippets plugin.

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

This will stop WordPress to delete your trash content by itself. So, you can manually empty the trash whenever you want or restore it for later use. You will be the BOSS

If you liked this article, then please follow us on Facebook for more WooCommerce and WordPress tutorials. You can also find us on Twitter.

What are your thoughts? Do you keep trash posts for a longer period of times? or You just get rid of them as soon as you can?

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *