Forked from anonymous/trim_drupal_node_revisions.sql
Last active
August 29, 2015 14:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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