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. You don’t have to change anything, the plugin works out of the box.
When someone tries to access an attachment page The Smart Attachment Page Remove Plugin causes an 404 error (“page not found”). Optionally, a status code 410 can be sent instead of status code 404. This is recommended.