wp_delete_post_revision()WP 2.6.0

Deletes a revision.

Deletes the row from the posts table corresponding to the specified revision.

Hooks from the function

Return

WP_Post|false|null. Null or false if error, deleted post object if success.

Usage

wp_delete_post_revision( $revision );
$revision(int|WP_Post) (required)
Revision ID or revision object.

Examples

0

#1 Delete revision with ID = 7

$rev = wp_delete_post_revision( 7 );

if ( $rev ){
	// revision deleted successfully
}
else {
	// no such revision was found
}
0

#2 Delete all revisions of the specified post

global $wpdb;

$postid = 5;

// get all the revisions of the post
$revision_ids = $wpdb->get_col( $wpdb->prepare( 
	"SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid 
) );

foreach( $revision_ids as $revision_id ){
	wp_delete_post_revision( $revision_id );
}

Changelog

Since 2.6.0 Introduced.

wp_delete_post_revision() code WP 6.5.3

function wp_delete_post_revision( $revision ) {
	$revision = wp_get_post_revision( $revision );

	if ( ! $revision ) {
		return $revision;
	}

	$delete = wp_delete_post( $revision->ID );

	if ( $delete ) {
		/**
		 * Fires once a post revision has been deleted.
		 *
		 * @since 2.6.0
		 *
		 * @param int     $revision_id Post revision ID.
		 * @param WP_Post $revision    Post revision object.
		 */
		do_action( 'wp_delete_post_revision', $revision->ID, $revision );
	}

	return $delete;
}