wp_scheduled_delete

The timeline below displays how wordpress function wp_scheduled_delete has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Permanently deletes comments or posts of any type that have held a status
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
 *
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' !== $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' !== $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($del_comment);
        }
    }
}

WordPress Version: 5.5

/**
 * Permanently delete comments or posts of any type that have held a status
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
 *
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' !== $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' !== $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($del_comment);
        }
    }
}

WordPress Version: 4.9

/**
 * Permanently delete comments or posts of any type that have held a status
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
 *
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' != $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' != $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($del_comment);
        }
    }
}

WordPress Version: 4.4

/**
 * Permanently delete comments or posts of any type that have held a status
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
 *
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' != $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' != $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($del_comment);
        }
    }
}

WordPress Version: 4.3

/**
 * Permanently delete comments or posts of any type that have held a status
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
 *
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' != $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' != $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($comment_id);
        }
    }
}

WordPress Version: 4.2

/**
 * Permanently delete comments or posts of any type that have held a status
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
 *
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
 *
 * @since 2.9.0
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' != $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' != $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($comment_id);
        }
    }
}

WordPress Version: 4.0

/**
 * Permanently delete posts, pages, attachments, and comments which have been
 * in the trash for EMPTY_TRASH_DAYS.
 *
 * @since 2.9.0
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' != $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' != $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($comment_id);
        }
    }
}

WordPress Version: 3.7

/**
 * Permanently deletes posts, pages, attachments, and comments which have been in the trash for EMPTY_TRASH_DAYS.
 *
 * @since 2.9.0
 */
function wp_scheduled_delete()
{
    global $wpdb;
    $delete_timestamp = time() - DAY_IN_SECONDS * EMPTY_TRASH_DAYS;
    $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $posts_to_delete as $post) {
        $post_id = (int) $post['post_id'];
        if (!$post_id) {
            continue;
        }
        $del_post = get_post($post_id);
        if (!$del_post || 'trash' != $del_post->post_status) {
            delete_post_meta($post_id, '_wp_trash_meta_status');
            delete_post_meta($post_id, '_wp_trash_meta_time');
        } else {
            wp_delete_post($post_id);
        }
    }
    $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
    foreach ((array) $comments_to_delete as $comment) {
        $comment_id = (int) $comment['comment_id'];
        if (!$comment_id) {
            continue;
        }
        $del_comment = get_comment($comment_id);
        if (!$del_comment || 'trash' != $del_comment->comment_approved) {
            delete_comment_meta($comment_id, '_wp_trash_meta_time');
            delete_comment_meta($comment_id, '_wp_trash_meta_status');
        } else {
            wp_delete_comment($comment_id);
        }
    }
}