Computer literacy, assistance and repair

How to delete wordpress revisions? Get rid of excess trash!!! How to disable, limit, delete WordPress revisions Delete directly in the database.

Hello dear readers. Today I'd like to talk about revisions in WordPress, how you can limit the number of times they are saved in the database per post or page, and how to disable and delete them completely.

What are editions and why are they needed?

Editorials (revisions) in WordPress- These are backup copies that are saved in the database every time a post or page is updated. On the one hand, this is convenient, since you can always restore a backup copy of an article at any time, because WordPress saves absolutely all backup copies. But let's imagine that you have a highly loaded project with high daily traffic and a huge amount of content. What then? Then the database may experience enormous loads. What can we do? If you still need revisions of posts and pages, you can limit the number of times they are saved, or you can completely disable and delete them, thereby reducing the load.

Limiting the number of revisions saved

The number of revisions in WordPress can be limited in two ways:

  1. Using the constant WP_POST_REVISIONS;
  2. Using the wp_revisions_to_keep hook (this hook also allows you to select the type of posts for which the restriction is set, be it standard or custom post types).

In order to limit the number of saving revisions using the WP_POST_REVISIONS constant, you need to configuration file wp-config.php (it is in the root of the site) add the following code:

Define("WP_POST_REVISIONS" , 1);

Now, for each post and page, one revision will be stored in the database.

As I wrote above, the wp_revisions_to_keep hook gives more options. Below is an example of code to limit the number of saved revisions with comments, which you need to add to your theme's functions.php file:

/** * Limiting the number of saving revisions using the wp_revisions_to_keep hook * @param integer $count - number of revisions * @param object $post - post object */ function limit_save_revisions_db($count, $post) ( if ($post->post_type = = "page") (//for standard WordPress pages, save 1 revision return 1; ) elseif ($post->post_type == "post") (//for standard WordPress posts, save 3 revisions return 3; ) elseif ($post ->post_type == "reviews") (//for the custom post type "Reviews" we do not save revisions return 0; ) else (//for all others we save 3 revisions return 3; ) ) add_action("wp_revisions_to_keep", "limit_save_revisions_db" , 10, 2);

Complete disabling and deleting revisions

If you decide to disable revisions on your site altogether, you can also use the wp_revisions_to_keep hook by adding the following code to your theme’s functions.php file:

/* * Total revision deactivations * @param integer $count - number of revisions */ function deactivate_revisions($count) ( return 0; ) add_filter("wp_revisions_to_keep", "deactivate_revisions");

In addition, after completely disabling editions, it is advisable to delete them from the database. After all, before the shutdown, they were still preserved and will now lie there as unnecessary “dead” weight. To do this, you need to go to PHPMyAdmin, find the desired database and open the wp_posts table in it. Next, click on the SQL tab and execute the following query:

DELETE FROM `wp_posts` WHERE post_type = "revision";

Now you need to delete all metadata (wp_postmeta table) and taxonomies (wp_term_relationships table) of the editions. To do this, we run 2 more queries:

DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = "revision" AND post_name LIKE "%revision%"); DELETE FROM wp_term_relationships WHERE object_id IN (SELECT ID FROM wp_posts WHERE post_type = "revision" AND post_name LIKE "%revision%");

Of course, it's best to back up your database before running these queries.

That's all. I hope the article was useful to you. Good luck to all!!!

Today's article will talk about WordPress revisions.

WordPress revisions are automatic saving of posts when editing. On the one hand, this is an excellent function: for example, you have been writing an article for more than an hour, when suddenly the electricity in the apartment is turned off, or the browser freezes, or the tab is accidentally closed... When this happened to me for the first time, thoughts immediately came to mind about how I’ll have to re-type the text, align the images, in general, do everything all over again, but no such luck! WordPress automatically saved my entire post! You won’t believe how grateful I was to the developers for such a feature.

But there is also another side to the coin. Default, WordPress revisions are done every 60 seconds and each revision is written to the database. Now calculate how many revisions you have on average per post and multiply by the number of blog posts. Wildly a lot! Half of the database size may consist of revisions. This is exactly what happened to me. See how the database size has decreased after deleting all revisions. Twice!

How to Disable WordPress Revisions

For disabling WordPress revisions open the wp-config.php file, it is located in the root of your site.

We go into it and add the following lines:

define("WP_POST_REVISIONS", 0);

The number in parentheses shows how many revisions are allowed for one entry. You can also specify the autosave interval in seconds (default 60).

What are WordPress revisions and why are they needed?

Audits– this is a backup saving of a post (post) in WordPress, which is performed automatically by the engine.

I think you have seen that when publishing a new post on a blog, in the admin panel, when editing a post, the “Publish” button becomes inactive from time to time. So, just at this moment wordpress performs an audit.

That is, in other words, if you edit a post in the admin panel, WordPress saves the old copy of this post to its database so that you can return to it at any time. He performs such saves once every 60 seconds. (this is the default, but you can set it to any time).

As you may have guessed, this is done for the purpose of protecting you from unexpected data loss. After all, no one is immune from force majeure; for example, I had a case when, when publishing a post, my browser closed by itself for some reason, and after starting it showed me a message about a fatal error. And naturally, in this case, all the sites that were in the tabs were closed, and accordingly all the information was lost. It is precisely in such situations that the revisions that WordPress makes save the day.

By the way, you can see the list of all revisions in the same place - in the WordPress admin panel. They are located just below the entry you are editing:

Here you may have a question: since revisions are so useful, why should they be deleted? Let me explain.

The fact is that such backup copies of posts significantly bloat the database; they, one might say, clog it with unnecessary garbage that needs to be gotten rid of. After all, think for yourself, when you need to make a lot of changes to a post, you edit it, and at the same time Wordpress saves old copies of posts one after another. These copies are added to the base and increase its size. But if you look at it like this, then we essentially don’t need them, well, maybe the last 2-3 copies, but not the rest. Therefore, naturally, you need to get rid of such things, in this case the database will become lighter and WordPress will work faster.

Removing wordpress revisions

Now there are several ways to delete revisions. You can delete backups manually, directly in the database, by entering a special SQL query, or you can perform this operation using specialized plugins. We will now consider all these methods.

First way– SQL query to the database. The first thing I recommend is to back up your database, in case you do something wrong, so that you can quickly restore all your data.

So, go to phpMyAdmin on the hosting, select the database for which you want to clear revisions.

After that, in the top panel, go to the SQL tab and insert this entry into the field that appears:

DELETE FROM wp_posts WHERE post_type = “revision”;

Database Query

If you do everything correctly, an inscription will appear in front of you, which will signal that revisions have been successfully deleted from the database. Message about successful execution of SQL query

Second way– We use the plugin Revision Control. Everything is simple here, first download the plugin, then install it. Installation field in the “Tools” panel, you will have another section - “ Revision Control", the plugin is very simple and I think it won’t be difficult for you to understand it.

That's all, the revisions have been deleted!!!

These are the two most basic ways to clear a database of revisions. There are, of course, several more plugins that allow you to perform this manipulation (more specifically, this is DelRevision And WP_Optimize) but I think that these two methods will be enough for you.

I would also like to say that you also have the opportunity to completely prohibit WordPress from making revisions. The truth here is that you should always keep in mind that in cases of force majeure, you will not be able to recover lost data.

In order to prohibit backup data storage, you need to perform a number of actions, namely:

Go to the hosting file manager (or contact the hosting via an FTP client).

Find a folder on the server wp-includes(This is usually located in the root directory of your site /yoursite/wp-includes.)

In folder wp-includes find file default-constants.php and open it in a text editor.

In this file find the inscription:

define('WP_POST_REVISIONS', true);

And replace it with:

define('WP_POST_REVISIONS', false);

That's it, after this WordPress revisions will be disabled!

When writing and editing posts in WordPress, they are automatically saved - a backup copy of the post (revision) is made. With the help of revisions, you can restore an article by going back a few steps. To restore an article, you only need to select the required revision.

However, revisions are not always good. For storage backup copies Posts and articles require additional server resources. Revisions clog up the database, so it’s worth considering whether they are needed at all. Do we often use revisions when editing articles in WordPress? If not, you can disable them.

Disabling revisions in WordPress

To disable revisions in WordPress up to version 3.0.3 you need to go to the configuration file “config.php” and open it with a text editor such as Notepad++. After this you need to find the line:

define("WP_POST_REVISIONS",0);

In case we have WordPress version 3.0.3 or newer — revisions are disabled in the “default-constants.php” file (located in the “wp-includes” folder). We are looking for the line:

define("WP_POST_REVISIONS", true);

After that, change true to false, as shown below:

define("WP_POST_REVISIONS", false);

Save the file “default-constants.php”. This way, revisions will be disabled.

All created revisions in WordPress can be easily deleted. In order to delete old revisions you need to go to phpMyAdmin and select a database. Then you need to go to the tab SQL and in the window that opens, insert the following line into the input field:

DELETE FROM wp_posts WHERE post_type = "revision";

An example is shown in the figure:

Then click the “OK” button and all previously saved revisions will be deleted. This method of deleting revisions is suitable for all versions of the WordPress engine.

Most WordPress users are not even aware of the concept of “WordPress revisions,” but it is useful to understand what they are, and in some rare cases, it is advisable to know how to disable them.

So, revisions (or editions) are copies of your posts that are created every time you save or autosave a page when it changes. This is done in case you want to return to some previous edition of the text or in case of an unexpected computer or communication failure.

I believe that if you do not have any problems in the operation of the site, it does not have a large database that you would like to reduce and there are no comments on the speed of page formation, you should not worry about the presence of editorial staff on the site.

You can see the generated revisions (in Russian WordPress terms) under the editing window.

By switching to any of the text revisions in the list, you will return the text to the state that corresponds to the time when this revision was saved.

As we can see, for each entry there can be many editions, and for a site with a large number of entries they can take up a significant amount of disk space, which can ultimately lead to problems in its operation.

For small sites this is not relevant, but if you have thousands of records and each one is represented in several editions, this can significantly increase the size of the database. Firstly, it slows down work with the database, secondly, these records take up space on the hosting, and thirdly, which, for example, was relevant for me, this may interfere with automatic backup your data. My site data is regularly automatically archived by the plugin and sent to a specific email. If the archive exceeds the size limit for Email This technology has stopped working.

Setting up revisions

How to remove Wordpress editions? First, you can specify the following instructions in the wp-config.php file (located in the root folder of your site):

define("WP_POST_REVISIONS", 0);

which means keeping only the three most recent revisions.

You can delete revisions that have already accumulated in the database in at least two ways.

1. Delete directly in the database

We go to phpMyAdmin and then go to the desired database. Then in the top menu go to the “SQL” tab. A window will appear in which you need to enter the SQL command:

And click the button below - “OK” (or “Forward”). That's it, the editions have been deleted.

Related publications