How to Modify Trash Handling in WordPress

When you delete a post or page in your WordPress, it is automatically moved to the trash. It is a quite user friendly solution, since you have 30 days to restore the trashed content. After 30 days, WordPress removes the deleted items from the trash and database too.

In this article I will write about how you can modify and customize the default trash handling of WordPress.

How to Delete the Trashed Content Immediately

Once you deleted a content, it appears in the trash. You can check these items by clicking on the trash link, at the top of your posts’ (edit.php) or pages’ (edit.php?post_type=page) list.

WordPress deletes the trash content after 30 days of deletion. If you want to delete the items in trash, you can do it one by one or in bulk. To delete any of the trashed items, you should move your mouse over the item where a “Delete Permanently” link appears. Click on it and the item will be deleted from trash and removed from database forever. To delete all trashed items, click on “Empty Trash” button at the top of list. This action empties the trash content and remove the items from database permanently.

How to Disable the Auto Emptying Trash Function

Of course, it is possible to disable the auto emptying trash function in WordPress. To do this, you should add a code piece to your functions.php:

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

With this solution you can easily disable the auto emptying trash feature of WordPress. Do not forget to check the trash content regularly, since your deleted items won’t be emptied from now, and it is highly important when you are working  together with other writers who produce trashed content on regular basis.

How to Alter the Auto Emptying Trash Function

You cannot just disable the auto emptying function of WordPress, but alter it as well. To do this, simply add this code piece into your functions.php:

define('EMPTY_TRASH_DAYS', 20);

where the number at the end shows the number of days while the trashed content must remain alive in trash.

How to Disable the Trash Function in WordPress

Even though disabling the trash function is not highly recommended, you can disable this function too. For instance, you may need this step when bulk deleting some spam content.

To disable the trash function you can use a little hack, since you should set the EMPTY_TRASH_DAYS variable to 0. This step implies to WordPress that all deleted posts, pages and comments should be removed immediately, without storing them in trash.

Once you perform this step, you will see a “Delete permanently” link instead of “Trash” link on your posts, pages and comments pages.

How to Empty Trash From Database

In some cases you can’t use the “Empty Trash” button. Deleting trashed content in bulk needs more time than deleting only few items and all webservers have limitations to run scripts. One of these limitations is called “maximum execution time” what sets the number of seconds a script is allowed to run. This value is limited to 30 seconds by default, so if the bulk deletion of trashed content would need more than 30 seconds, the script stops. In some cases the whole WordPress back end stops too.

To avoid the deletion of trashed content manually, one by one, you can delete the trashed content from database. This step needs some advanced help, like asking a server administrator or using PHPMyAdmin software. It is also highly recommended to backup the database before performing any manual actions, since databases are quite sensitive and errors in them could stop the whole website.

So, if you are performing the database altering yourself, you should perform this query:

DELETE p
FROM wp_posts qposts
LEFT OUTER JOIN wp_postmeta qpostmeta ON (qposts.ID = qpostmeta.post_id)
WHERE post_status = 'trash'

With this step your database will be cleaned from trash entries and also your WordPress trash will contain no entries.

Plugins to Empty your WordPress Trash

Of course, there are many WordPress plugins what can help you to empty the trash easier and more effectively.

Quick Empty Trash

Quick Empty Trash is one of the simplest plugin to empty the WordPress trash. Its usage is quite easy. Just download this plugin here.

and install it to your WordPress system. Activate it and then the default WordPress “Empty Trash” button appears  on your posts and pages.

Since the Quick Empty Trash plugin is so simple, there are no advanced settings or any customization methods.

Auto Trash Delete

Auto Trash Delete is a quite new plugin, it can be used to set the days to keep in trash value. To add this plugin, you should download it here.

Once you activated it, a new submenu appears in WordPress admin’s Settings: Auto Trash Settings. There you can see an input field where you can set the number, while WordPress keeps the trashed elements alive.  To put it more simply, this is the place where you can alter the above mentioned EMPTY_TRASH_DAYS value without editing manually the functions.php file.

And that’s all, there are no more settings or advanced customization options in this plugin.

Batch Comment Spam Deletion

Batch Comment Spam Deletion is not a plugin to empty your trash, but it helps you remove the spams from your database. This plugin is mentioned here to help you cleaning up your database, as a  special kind of trashing process.

As a first step, you should download the plugin here.

After activation, go to your Comments page in backend and click on spam link. This way you can see the spammed comments.

Thanks to Batch Comment Spam Deletion plugin, by clicking on “Empty Spam” button you won’t run the usual WordPress deletion process. The plugin will run the process in batches instead, this way it won’t exceed the maximum execution time of your web server.

This is a super easy and effective way to avoid spammed comments without killing the server’s resources.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditPin on Pinterest