Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deekayen/c4c3520de268ec83e078 to your computer and use it in GitHub Desktop.
Save deekayen/c4c3520de268ec83e078 to your computer and use it in GitHub Desktop.
Delete revisions from the Drupal 7 node_revisions table such that only the newest 3 revisions remain for each node. It joins on the node table to make sure that the current, active vid assigned in the node table doesn't get deleted.
DELETE FROM node_revisions WHERE vid IN (
SELECT subquery.vid FROM (
SELECT @row_num := IF(@prev_value=nr.nid,@row_num+1,1) AS RowNumber
,nr.nid
,nr.vid
,nr.timestamp
,@prev_value := nr.nid
FROM node_revisions nr,
(SELECT @row_num := 1) x,
(SELECT @prev_value := '') y
ORDER BY nr.nid, nr.timestamp DESC
) subquery LEFT JOIN node n ON n.vid = subquery.vid
WHERE RowNumber > 3 AND n.nid IS NULL
)
@deekayen
Copy link
Author

deekayen commented Oct 3, 2014

Don't forget that content types with additional fields may have vids associated in other tables. They should be able to be added as a comma delimited list

DELETE FROM node_revisons, field_revision_field... WHERE vid...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment