A WordPress media library cleanup sorts images into keep, verify, and archive decisions while checking page relationships

Editorial illustration; the media library and review flow are representative and are not a live WordPress installation.

Every long-running WordPress site eventually turns its wp-content/uploads directory into an attic. You inherit a client site before a hosting migration and find 14 GB of old campaign banners, five variations of the company logo, Screenshot 2021-04-12 at 3.14.22 PM.png, and uncompressed 8 MB originals uploaded straight from a DSLR.

The first instinct is to open the Media Library, filter by Unattached, select all, and hit delete.

Do not do that. On a real WordPress site, “unattached” is one of the most misleading labels in the dashboard. Deleting everything in that filter is the fastest way to break a homepage hero, a WooCommerce category banner, or a footer logo that a client only notices two weeks later.

Why “Unattached” Means Almost Nothing

In WordPress, a media file is stored as a row in wp_posts with post_type = 'attachment'. When someone uploads an image directly inside the Block Editor while editing a specific post, WordPress sets post_parent to that post’s ID.

When someone uploads an image directly through Media → Add New, or selects an existing image from the library to use on a different page, post_parent stays 0. WordPress marks it as Unattached.

That means an image used in your active Elementor template, an ACF options page, a theme customizer logo slot, a CSS background-image rule, or a WooCommerce product gallery can still show up as unattached. Even worse, deleting the original attachment record deletes all generated thumbnail sizes (-150x150, -1024x768, .webp derivatives) from disk at the same time.

Start With Storage Reality, Not the Media Grid

Before touching a single attachment, figure out what you are actually trying to fix. If the goal is to slim down a bloated backup or cut hosting disk usage, deleting random 40 KB blog thumbnails in the UI will waste your afternoon.

SSH into the server and check where the gigabytes actually live:

du -sh wp-content/uploads/* | sort -h

On older Malaysian SME or agency sites I have audited, the biggest culprits are rarely normal blog images. They are usually:

  • Backup plugins (UpdraftPlus, All-in-One WP Migration) storing .zip or .wpress archives inside wp-content/uploads/.
  • Old year folders (2018/, 2019/) from a previous theme that registered 15 custom thumbnail sizes.
  • Uncompressed PDF brochures or high-resolution product photos uploaded at 5000px wide.

Before running any cleanup, take a full database dump and a file backup you have actually tested:

wp db export pre-media-cleanup-$(date +%F).sql

If the site is behind Cloudflare or a caching plugin, note that cached HTML can mask broken images for hours or days after deletion. Plan to purge relevant caches and inspect pages in an incognito window.

Tracing Where an Image Actually Lives

If you want to know whether a file is genuinely unused, checking post_parent is not enough. You need to search across post content, post meta, site options, and theme files.

With WP-CLI, you can check how many unattached items exist first:

wp post list --post_type=attachment --post_parent=0 --format=count

For suspicious or large files you want to verify before removing, search the database by both filename and attachment ID. Page builders like Elementor or Divi store URLs in JSON inside wp_postmeta, while ACF and WooCommerce often store the numeric attachment ID instead of the filename.

# Check post content and postmeta for a filename
wp db query "SELECT ID, post_title, post_type FROM wp_posts WHERE post_content LIKE '%old-promo-banner%';"
wp db query "SELECT post_id, meta_key FROM wp_postmeta WHERE meta_value LIKE '%old-promo-banner%' LIMIT 25;"

# Check wp_options (theme customizer, widgets, site logo, header/footer builders)
wp db query "SELECT option_name FROM wp_options WHERE option_value LIKE '%old-promo-banner%';"

Do not forget the filesystem itself. Older custom themes love hardcoding /wp-content/uploads/... inside style.css or header.php:

grep -rn "old-promo-banner" wp-content/themes/ wp-content/plugins/

If all four checks come back empty, and the attachment ID is not referenced in wp_postmeta (for example in _thumbnail_id or _product_image_gallery), you have strong evidence—though still not a 100% guarantee if an external email newsletter or PDF links to that URL.

Archiving Before Deleting

When a client has five years of uncertain assets, the safest move is a staging quarantine rather than immediate wp post delete.

For files that look obsolete—retired Ramadan or Merdeka campaign banners from three years ago, draft post images for posts that were moved to Trash, or exact duplicates—work in small batches. Keep a simple CSV or markdown log with the attachment ID, original URL, and date removed.

If you are cleaning up orphaned files on disk (files sitting in wp-content/uploads/ that have no database record at all), move them to an archive directory outside the web root first instead of running rm -rf:

mkdir -p ~/uploads-quarantine/2026-11
mv wp-content/uploads/2020/03/retired-banner* ~/uploads-quarantine/2026-11/

Then watch your web server logs or Cloudflare analytics for 404 responses on /wp-content/uploads/:

tail -f /var/log/nginx/access.log | grep ' 404 ' | grep 'uploads'

If nothing breaks after two or three weeks of normal traffic—including weekly email blasts and scheduled posts—you can safely purge the quarantine folder.

Keeping the Library Clean Going Forward

Cleaning up 4,000 old files is painful once; doing it every year means the editorial workflow is broken. Two small habits prevent most of the mess:

  1. Enforce sensible upload limits and auto-scaling. WordPress already scales down huge images above 2560px by default, but PDFs and PNG screenshots slip through. Converting heavy screenshots to WebP or JPEG before upload saves far more space than deleting old thumbnails later.
  2. Name files by purpose, not camera defaults. kuala-lumpur-office-map.webp is easy to audit two years from now. IMG_4821-1-scaled.jpg forces whoever maintains the site next to play detective.

For related guides that complement this checklist, see:

Sources