It would be easier to use a plugin for this. I’ve been using this one for similar task https://wordpress.org/plugins/media-cleaner/. It missed some files, but removed majority of the “orphaned” images though.
Related Posts:
- Stop wordpress from hardcoding img width and height attributes
- how to get original image using wp_get_attachment_image_src
- Add new image size to media uploader so users can insert into post?
- How to insert pictures without hard coded dimensions?
- How to modify image editor dialog options on WordPress?
- Is there a hook which fires after all thumbnails are generated?
- “There has been an error cropping your image” when cropping image
- Apply image width to tag
- Excludes posts that don’t have attachments in the_content()
- Remove duplicate images from media library.
- WordPress Image Editor doesn’t update thumbs specified with `add_image_size()`
- How can you upload an image from within a settings page?
- Get an image from url in front end submission form
- Can images be automatically compressed?
- Why would image paths in wordpress suddenly change?
- Add visual editor to Media Edit Screen
- Show prev and next post links for parent post of current image in attachment page?
- could not upload image in media library
- Check if page/post has any anchors that link to an image jpg/gif/png
- How to check if user is uploading/setting an image as a featured image?
- hardcrop images in gutenberg “latest posts” block
- Use Gravatar as fallback image if no local image is not found
- add the post as canonical for attachment page wordpress
- Retrieving an alt tag from a custom field
- Exclude featured image from gallery in wp-admin
- Trying to create a mouseover effect in html using Gutenberg editor
- wordpress get gallery images title
- How to fix “.webp” being auto-added to the end of image urls
- Customizing the attachement page to show rest of all images in a gallery from post
- Changing default img html markup but not in admin back-end?
- Retrieve an array of image URLs that belong to a gallery
- Change align classes for images
- Ho to add “Create Slider” option to default “Add Media” popup?
- Images in Blog List
- Using featured img as div background
- How can I change get_the_post_thumbnail to make 100% width image?
- Native gallery sorting is ignored
- Display an icon with get_post_meta?
- Include WooCommerce 2’s prettyphoto scripts on all site pages
- Get custom thumbnail sizes [duplicate]
- Using Jetpack carousel w/o creating a gallery [closed]
- Where do I locate theme image files
- WordPress Facebook Open Graph plugin not working for 1 specific image [closed]
- Custom responsive ‘featured image’ sizes
- removing the featured image from the post
- Dynamic image crop in WP 3.0+?
- What can i do add title to ‘large’ image?
- Warning : tempnam() [function.tempnam]: SAFE MODE Restriction in effect
- Image text wrap not working properly on frontend
- WordPress cutting images size
- How to set minimum required size for featured image?
- Errors and warning with padlock sign when site moved to SSL
- How to make footer area transparent? Using Avada and WordPress [closed]
- Whats causing image quality to be degraded?
- Exclude image url from search query
- Fancybox just loading small white square
- WordPress Resize Image and show as thumb in post
- Resize uploaded images [duplicate]
- Image “description” and SEO within WP
- Link to full-size image for subscriber only
- Indicate image sizes for upload by post type
- Why can’t I gain access to my homepage images?
- How to track inexistent images request
- How to change Gallery image url in product page ? In function.php?
- Display image of child element in wordpress
- How do you obtain a list of all image file names on a wordpress website for replacement?
- Form File Upload Field generates multiple copies of Image Files (different sizes): Is this a WP feature or is this being done by the Plugin
- How to get title of images in post content
- Upload error on localhost (at minimum, not yet tested online)
- Text Wrapping in Editor not Matching Website View
- Duplicate images on upload
- Broken images on iphone
- Redirect when user clicks on an image
- JPG Image extension not inserting into Custom table of wp
- wp_get_attachment_image_attributes not working
- Exclude first image from a Woocommerce image gallery
- Adding custom html/css/images to Theme
- Responsive images with srcset
- Use wget to find used images
- How to handle a lot of media files in the uploads folder? (+10.000)
- How to enable Modal Image in DWQA Plugin of WordPress?
- How to link to img src using HTML email template in WordPress
- How to add an image below the header on homepage
- How can I add bigger image in my blog post?
- Relative path not working at all in WAMP local environment
- Images in plugin broken after URL migration
- Gallery Images could not be shown
- SVG files and wordpress
- Different image margins on different pages CSS [closed]
- Small image resizing for big! Alternative to timthumb
- Reduce Mobile Image upload size (wordpress)
- Gallery with shortcode not showing
- Show custom image size smaller in editor
- Check if post has image
- How to get children/attachments in header?
- Setting name of attachment URL
- Show title when inserting single images
- How do I mitigate the forward slash problem for URLs of images after a WP migration?
- WordPress file browser
- Best Photoshop-WordPress workflow for editing images without re-upload [closed]