By john


2011-11-30 22:22:46 8 Comments

I have a drupal 7 sites, where it keeps node and field revisions. I keep revisions for the case where by mistake a new faulty version is saved and I want to revert back.

But, now I want to clean and optimize the database so to take less space in hard disk.

What are some methods of deleting all the revisions except the current one, for all nodes/fields of the site?

5 comments

@tostinni 2011-11-30 23:21:46

There's not yet a stable release for Revision deletion but a D7 port is on its way (not sure if its functional at all).

In this thread someone suggested using VBO to isolate revisions and delete them.

If you feel comfortable with the API, make a loop through all your nodes, retrieves the revisions using node_revision_list() and delete them using node_revision_delete().

As a last resort and if you like messing with the DB (like I do) you can delete revisions' rows in field_revision_* and node_revision tables. Try this:

WARNING: not tested at all !

DELETE FROM field_revision_body
WHERE NOT EXISTS (
  SELECT NULL
  FROM node
  WHERE vid = revision_id
);

DELETE FROM node_revision
WHERE NOT EXISTS (
  SELECT NULL
  FROM node
  WHERE node.vid = node_revision.vid
);

You can find the complete list of your revisions' tables in the information_schema database:

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE 'field_revision%'
AND TABLE_SCHEMA = 'your_database';

@Free Radical 2014-05-13 10:14:12

Node Revision Delete module now has a stable release.

@digitgopher 2016-01-06 18:51:55

Testing this, I believe the condition on node_revision query should be WHERE node.vid = node_revision.vid. Errors as is. Perhaps specify the tables on the field_revision_body query as well?

@mbomb007 2019-07-03 16:50:28

The Revision Deletion module mentioned in this answer recommends using Node Revision Delete instead, since we don't need 5 modules doing the same thing. See accepted answer.

@Juampy NR 2014-04-29 11:29:44

Node Revision Delete module now has a stable release.

The Node Revision Delete module lets you to track and prune old revisions of content types. Features:

  • Define the max amount of revisions to keep per content type.
  • Run on Drush, cron run or on a different period (daily, weekly, etc).

@Kaushal Kishore 2012-12-04 11:02:38

Use the Node Revision Delete module to delete and manage your revisions. (I am the maintainer of the module.)

The Node Revision Delete module allows you to manage the revisions of the Node according to your choice. It helps you to keep the specific number of revisions for the node. This module provides you the flexibility for applying the revision delete for the specific content type and run it on the specific time. You can manage your settings from the Node Revision Delete Administration Page.

@mrded 2012-12-03 15:32:28

In same situation I used code below:

foreach (node_load_multiple($nids) as $node) {
  // Delete sub revisions.
  foreach (node_revision_list($node) as $vid => $revision) {
    node_revision_delete($vid);
  }
  // Delete archived revisions.
  revisioning_delete_archived_revisions($node);
}

@Merrick 2012-05-11 02:49:00

I made this for the VBO folks (execute arbitrary PHP), it will delete all revisions (except current). This relies on the sort order built into node_revision_list(), so back up your DB & test for desired results before using on live data.

  krumo($entity);

  $revision_array = node_revision_list($entity);  
  $revision_array_ASC = array_reverse($revision_array, TRUE);
  krumo($revision_array_ASC);
  krumo(count($revision_array_ASC));

  foreach ($revision_array_ASC as $vid => $node) {
    $count = count($revision_array_ASC);
    if ($count > 1) {
          node_revision_delete($vid);
    }
  }

  $revision_array = node_revision_list($entity);  
  krumo($revision_array);

@dxvargas 2015-04-17 08:47:46

Seems like you are having a big trouble to avoid deleting the current revision (sort, count). But node_revision_delete already prevents deleting the current revision.

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] How to get the revisions list of a node and delete them all?

  • 2017-02-15 15:18:44
  • Daniel
  • 6630 View
  • 7 Score
  • 5 Answer
  • Tags:   nodes 8

0 Answered Questions

Block setting Feature keeps setting visibility to FALSE

  • 2018-08-22 13:53:52
  • user1015214
  • 13 View
  • 0 Score
  • 0 Answer
  • Tags:   7 features

0 Answered Questions

Optionally bypass required fields for content type

  • 2018-06-07 16:41:09
  • UnsettlingTrend
  • 44 View
  • 0 Score
  • 0 Answer
  • Tags:   8 nodes forms

1 Answered Questions

[SOLVED] Is it safe to delete all revisions of a deleted node?

  • 2018-03-31 15:50:52
  • Rosamunda
  • 64 View
  • 2 Score
  • 1 Answer
  • Tags:   nodes

4 Answered Questions

[SOLVED] How to delete node revisions

  • 2012-08-19 06:36:55
  • user8012
  • 4249 View
  • 7 Score
  • 4 Answer
  • Tags:   6 nodes

2 Answered Questions

'path' in array with special characters using imagecache

  • 2014-10-21 23:48:44
  • Collins
  • 45 View
  • 0 Score
  • 2 Answer
  • Tags:   7 blocks media

1 Answered Questions

[SOLVED] EntityFieldQuery: Load all revisions

1 Answered Questions

[SOLVED] Migrate doesn't update destination files if source files have changed content

  • 2013-02-13 16:39:55
  • ermannob
  • 258 View
  • 0 Score
  • 1 Answer
  • Tags:   7 files migration

1 Answered Questions

[SOLVED] Managing modules - site overriding

  • 2012-06-06 14:44:01
  • wtk
  • 41 View
  • -2 Score
  • 1 Answer
  • Tags:   7

1 Answered Questions

[SOLVED] How to list all inactive modules on a multisite?

  • 2011-07-14 15:35:25
  • Vacilando
  • 397 View
  • 5 Score
  • 1 Answer
  • Tags:   7 multi-sites

Sponsored Content