Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2014 17:58
Show Gist options
  • Save anonymous/9d54cc1fb6cb74372353 to your computer and use it in GitHub Desktop.
Save anonymous/9d54cc1fb6cb74372353 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
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment