Why the trash is not a safe place to move content to

Two weeks ago, I wrote the blog post celebrating 16 years of working with and blogging about WordPress. You’d think that after such a long time, you have seen everything in WordPress, especially all the “old stuff”. But you have no idea how much there is!

This week, I’ve found some piece of code, that really surprised me. I wanted to cover that in a blog post, but forgot to write it down. Trying to find that code again, I found something even more mind-blowing, which I want to cover today.

The trash as a “safe place”

We all do that. We write some content, are not happy with it, and move it to the trash. Most desktop operating system have a trash as well. Mine has currently only three file in it, all deleted in the last two weeks. But when I was using Windows as my daily OS, I may have hundreds of files in it, sometimes wasting several GB of storage. It was often safe to just empty the trash, but in some cases, you would find files, you didn’t want to delete in the first place. So it was reassuring to know, that the trash in Windows (and many other systems as well), does not get deleted automatically. That’s not the case in WordPress! 😮

Trashing things in WordPress

There are two things you can trash: posts/pages (and other post types) and comments. But did you know that they get deleted after 30 days? I did not know that!

The EMPTY_TRASH_DAYS constant

When trying to find the code for this other topic, I found the EMPTY_TRASH_DAYS by accident. It has a default value of 30, which means that any post or comment, that has been trashed at least 30 days earlier, will be deleted. This is done by the wp_scheduled_delete WP-Cron event, that runs daily.

When you set the constant to 0 (or another “falsy” value), you basically disable the trash altogether, and all “Trash” links in the backend will become “Delete Permanently” links.

When you trash a post or comment, WordPress will add two meta values _wp_trash_meta_time, with a timestamp on when the entry was trashed and _wp_trash_meta_status, reflecting the previous post or comment status, since they change to trash, and WordPress will restore the previous status, if you restore an entry from the trash.

Conclusion

If you are using your trash to “move things out of sight”, be aware, that they will be deleted after 30 days. If you want to extend that time, simply define the EMPTY_TRASH_DAYS constant in your wp-config.php with a (much) higher values.

Have you known this constant before reading this blog post? Have you ever used it, and if so, have you increased or decreased the time, or rather used it to disable the trash completely?

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

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