Skip to content

Instantly share code, notes, and snippets.

@lesterchan
Last active August 29, 2015 14:15
Show Gist options
  • Save lesterchan/07f43381d3459d16a950 to your computer and use it in GitHub Desktop.
Save lesterchan/07f43381d3459d16a950 to your computer and use it in GitHub Desktop.
Delete WordPress Revisions
<?php
/**
* Delete revisions from WordPress
*/
define( 'WP_USE_THEMES', false );
if( ! empty( $_SERVER['WP_DIR'] ) ) {
require( $_SERVER['WP_DIR'] . '/wp-blog-header.php' );
} elseif( is_file( 'wp-blog-header.php' ) ) {
require( 'wp-blog-header.php' );
} else {
die( 'Unable to find wp-blog-header.php' );
}
global $wpdb;
$limit = 600;
$date = '2015-01-01 00:00:00';
$start = time();
$revision_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE (post_type = 'revision' OR post_status = 'auto-draft') AND post_date < '$date' LIMIT $limit" );
foreach ( $revision_ids as $revision_id ) {
wp_delete_post_revision( $revision_id );
}
echo '<p>' . date( 'Y-m-d H:i:s' ) . ': ' . count( $revision_ids ) . ' revisions deleted. Took ' . ( time() - $start ) . 's.</p>';
$start = time();
$auto_draft_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft'" );
foreach ( $auto_draft_ids as $auto_draft_id ) {
wp_delete_post( $auto_draft_id );
}
echo '<p>' . date( 'Y-m-d H:i:s' ) . ': ' . count( $auto_draft_ids ) . ' auto-draft deleted. Took ' . ( time() - $start ) . 's.</p>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment