One of the problem with WordPress is when you delete a post, its attachments will not be deleted, i.e. if the post has attached images they’ll remain on the server and file system
Note: Use this function and logic only and only if you need to delete posts’ attachments for sure. If an attachment could be used in several different posts do not use the function!
This is solution for deleting attached images when deleting a post. Add the following code in your theme’s functions.php file:
function delete_post_attachments($post_id) { global $wpdb; $sql = "SELECT ID FROM {$wpdb->posts} "; $sql .= " WHERE post_parent = $post_id "; $sql .= " AND post_type = 'attachment'"; $ids = $wpdb->get_col($sql); foreach ( $ids as $id ) { wp_delete_attachment($id); } } add_action('delete_post', 'delete_post_attachments');