You can do it with native WordPress codex calls like this
( source http://badlywired.com/technical-stuff/2014/11/10/code-to-delete-all-images-from-wordpress-or-nearly-all/ )
<?php
/* create this code in a file in the main wordpress directory e.g. delmedia.php
and access it via mydomain.com/delmedia.php
*/
// Include the wp-load'er
include('wp-load.php');
$args= array(
'post_type' => 'book', // obvious
'posts_per_page' => -1 // get them all
);
// get all attachments post ids
$posts = get_posts( $args );
foreach ($posts as $post_id) {
// get an array of image data
$image_attributes = wp_get_attachment_image_src( $post_id->ID );
if (strpos($image_attributes[0], 'mystring') !== FALSE){
echo 'Image Found : '.$image_attributes[0];
if (false === wp_delete_attachment( wp_delete_attachment( $post_id->ID, true ) ) ) {
echo ' and delete failed!<br>';
} else {
echo ' and delete succeeded!<br>';
}
}
}
?>