Auto delete WordPress images/thumbnails (all sizes) after X days/hours, or similar?

I am looking for solution to auto delete images of old posts in WordPress site.
I want to hold images of current 50 posts, others should be deleted automaticly.
It can be function to delete on time interval, or function to hold only images of last 50 posts.
Does anyone know function or some plugin which can do this or similar things, to automaticly ddelete older images?

Related posts

Leave a Reply

1 comment

  1. http://wordpress.org/plugins/cleanup-images/

    or in code add hook with delete post

    function delete_post_media( $post_id ) {
    
        $attachments = get_posts( array(
            'post_type'      => 'attachment',
            'posts_per_page' => -1,
            'post_status'    => 'any',
            'post_parent'    => $post_id
        ) );
    
        foreach ( $attachments as $attachment ) {
            if ( false === wp_delete_attachment( $attachment->ID ) ) {
                // Log failure to delete attachment.
            }
        }
    }
    

    try this

    hope it will use full for you.