How to Rebuild WordPress Media Database (Guide)


You’ve just imported your content, but your images are gone. You check your server via FTP, and all the files are right there in the wp-content/uploads folder, but your WordPress Media Library is empty. The problem isn’t with your files; it’s with your database. The database has lost the crucial links, or “references,” that tell WordPress where your media files are located. When this happens, you need to rebuild WordPress media database to re-establish the connection. This technical guide will walk you through a safe, simple, and effective process to get your images back.

A screenshot of a WordPress plugin interface used to rebuild the media database.

The Cause: Database De-synchronization

Every time you upload an image through the WordPress dashboard, two things happen:

  1. The image file is saved to your server’s wp-content/uploads folder.
  2. A new entry is created in your database, which includes the file path, metadata, and attachment ID.

These two actions must be in perfect sync. If you manually transfer files via FTP, or if a migration tool fails to correctly copy the database tables, this sync is broken. The result is a library full of “unattached” media. While a manual fix is possible for one or two files, it’s a huge undertaking for an entire library. The safest and most efficient solution is a specialized plugin.

The Recommended Solution: A Purpose-Built Plugin

For this technical problem, we recommend using a purpose-built plugin like Media Library Recovery. It’s designed to do the heavy lifting for you by scanning your server and automatically creating the correct database entries.

Step 1: Back Up Your Entire Site

Before you touch your database, you must create a full backup of your entire website—both files and database. Use a reliable backup solution like Duplicator or UpdraftPlus to ensure you can revert your site if anything goes wrong. This is the single most important step.

Step 2: Install and Activate the Plugin

From your WordPress dashboard, navigate to Plugins > Add New. Search for “Media Library Recovery,” install it, and activate it.

Your Guide to Rebuild WordPress Media Database

Once the plugin is activated, navigate to Media > Media Recovery.

  1. Run the Scan: The plugin’s interface will immediately begin to scan your wp-content/uploads folder. It will show you a list of all media files that are on your server but are missing from your WordPress database.
  2. Verify the Files: Take a moment to review the list. This is a great way to confirm that the plugin has found all the images you were expecting it to.
  3. Initiate Recovery: Select all the images you want to recover. Then, click the Recover button. The plugin will now create a new database entry for each selected image file.

Why This Method is Best to Rebuild WordPress Media Database

  • Safety: A manual database edit carries a high risk of corruption. This plugin works by creating new database entries without altering existing ones, which is much safer.
  • Efficiency: The process can process thousands of images in a matter of seconds, saving you from a tedious and error-prone manual task.
  • Metadata: The plugin will also attempt to add common image metadata, such as file names and dimensions, to the new database entries, restoring the functionality you’d expect from a properly linked image.

What to Do After Recovery

Once the plugin has finished, you can check your Media Library again. All the previously missing images should now be visible and usable in your posts and pages. You may also need to:

  • Clear Your Cache: Your browser, caching plugin (WP Rocket), or CDN (Cloudflare) might still be showing you an old version of your site. Clear all caches to ensure you are seeing the most recent changes.
  • Fix Broken Links: If you had a site migration, some of your content might have broken links to your old domain. You may need to run a search and replace to fix those.

Advanced Technical Notes (for Developers)

While a plugin is the recommended solution, it’s helpful to understand the technical process. The plugin is essentially performing a series of operations similar to this pseudo-code:

  • $files = scandir('wp-content/uploads')
  • foreach ($files as $file):
    • if (!$file_exists_in_database($file)):
      • $attachment_id = wp_insert_attachment($file, $post_id)
      • generate_attachment_metadata($attachment_id)
  • This process bypasses the front-end upload and directly interacts with WordPress core functions to create the necessary database entries.

Conclusion

Having a de-synchronized database is a frustrating technical problem, but it has a simple and powerful solution. By using a specialized plugin, you can easily rebuild WordPress media database, ensuring a perfect sync between your file system and your database. This streamlined workflow saves you from the tedious and risky task of manual re-uploads, allowing you to get your site back in perfect working order in minutes.