Delete posts with word count less than x number of words

function delete_posts() {
$lastposts = get_posts(array('numberposts' => -1));

if ( $lastposts ) {
    foreach ( $lastposts as $post ) :
        setup_postdata( $post ); ?>        
        <?php
        $content = get_the_content();
        if (str_word_count($content) < 100) {   
            wp_trash_post($post->ID);
        }

        ?>
        <?php
    endforeach; 
    wp_reset_postdata();
}}add_action( 'init', 'delete_posts' );