Sébastien Laframboise

Clean the WordPress media library without deleting files that are in use

Clean the WordPress media library without deleting files that are in use

You can clean the WordPress media library without breaking your pages, but treat each «unused» file as a candidate to check, not as a file to delete automatically.

A media item can be used in a post, a featured image, a site template, a product gallery, a custom field, or a download link. Also, WordPress can delete the physical file when you permanently delete its attachment. So caution is essential.

Why an orphaned media item isn’t always truly unused

In WordPress, a media item is an attachment. The media library lets you view its details, file URL, and delete it. However, the apparent lack of association with a post doesn’t prove the media is no longer displayed on the site.

The «uploaded to» status is helpful for guidance, but it’s not enough to decide. An image may have been added to a page after upload. It can also be reused elsewhere without changing that initial association.

Image blocks can store a URL and a media ID inside their content. Thus, checking only the file title or the association column can miss an image that’s actually in use. See the WordPress Image block documentation to understand the attributes used by that block.

Finally, a visually duplicate image can have a distinct role. For example, two similar files may have different crops, filenames, alt text, or locations. Delete only what you can confirm.

Prepare a safety net before cleaning

Start by creating a a full backup of the files and the database. A database-only copy isn’t enough because media are usually stored in the site’s files.

Also verify that you know how to restore this backup. For a structured method, see how to create a reliable automatic WordPress backup, then test restoring a WordPress backup before an emergency.

If your host provides a staging environment, do the cleanup there first. You can then spot a missing image without exposing visitors to a broken page.

Establish a short checklist

  • Note the date and keep a recent backup.
  • Work with an administrator account.
  • Choose a small range of media to review.
  • Keep a list of the files removed and their URLs.
  • Plan to check the site after each batch.

This preparation slightly slows the first pass. However, it greatly reduces the risk of having to restore the entire site for a single deleted PDF or image.

Find cleaning candidates in the media library

In Media > Library, use the filters by type and date. Switch to List view when you want to compare multiple files quickly.

Prioritize items that are easy to explain: upload tests, temporary captures, replaced documents, images imported by mistake, or visuals created for a finished campaign. Conversely, avoid starting with logos, product photos, client documents, and images of important pages.

Then open each selected item. The media details panel shows the file name, alt text, and direct URL. The official Media Library documentation also confirms that this panel provides access to an item’s details and deletion.

Duplicates require a separate review

Do not delete a duplicate just because its name contains «-1», «copy» or a date. Open both files and compare their content, dimensions, URL, and where they’re used.

When two media files are truly identical, prefer keeping the one already linked to published content. First replace references to the other file if needed. You can then remove the now-unneeded candidate.

If your need is more about compression, format, or daily organization of images, also check these WordPress plugins for managing your images. Media cleanup mainly aims to remove unnecessary files, not to optimize every image.

Check where a media file might be used

Before deleting a file, copy its URL and search for its name in relevant content. Then check places that often use media without showing it clearly in the «Uploaded to» column.

Pages, posts and templates

  • Open the pages and posts that might use the visual.
  • Check the featured image of each important piece of content.
  • Examine theme templates and template parts, especially the header and footer.
  • Check site editor compositions, reusable blocks, and banners.

Store, fields and downloads

If you use WooCommerce, check products before deleting anything. A product image can be the main image or part of a product gallery. WooCommerce states that product images and galleries are selected from the media library and reused in multiple sizes across the store.

Refer as needed to the WooCommerce documentation on product images and galleries. Also check variations, custom fields, widgets, theme settings, and plugins that manage galleries.

Pay special attention to PDFs, ZIP files, and downloadable documents. A page can contain a direct link to their URL without the file appearing as an image in the editor.

Delete progressively, then verify the result

Process a small batch at a time, for example five to ten items you’ve already checked. After each batch, open the site’s main pages in a private window. Check the homepage, service pages, recent posts, forms, and product pages.

When a media item is permanently deleted, WordPress also removes its file and the data related to that attachment. The technical reference wp_delete_attachment() describes this behavior and explains that WordPress also removes the associated metadata.

So avoid directly deleting database rows or files in the uploads. folder. You could leave inconsistent references or make restoration more difficult. Use the media library for normal deletions.

After your checks, clear or refresh the site cache if your setup uses one. Then reload the pages you checked. A cache can temporarily hide a missing image or, conversely, still show an old file.

A safer maintenance method than a large-scale cleanup

The best cadence is to clean the media library regularly, in small batches. That way you retain the context of each recent campaign, import, or update.

Add this task to your’monthly WordPress maintenance. routine. You’ll avoid accumulation while protecting images and documents that still support your content.

If you’re unsure about a single file, keep it. The space saved by an uncertain media item is rarely worth the risk of breaking a page, gallery, or useful download.