This free WordPress Plugin allows you to remove Attachment Pages automatically generated by WordPress
WordPress automatically generates a page for every file you upload to your Media Library. Even if you do not want to use these Pages and never link media files to their attachment page these pages exist and can be accessed by their automated generated URL.
The smart Attachment Page Remove Plugin blocks access to Attachment Pages and causes an 404 page not found error (optionally an 410 error can be sent).
You don’t have to change anything, the plugin works out of the box.