Restoring Deleted Media Library Files in WordPress

The WordPress media library can be a frustrating experience. It doesn’t have many features that big sites would deem essential, such as a logical way to order files or a trash, so that files don’t get accidentally deleted.

Developers will know this all too well – a client accidentally deletes an image that is used in a million places on the website. Sure, you could restore a backup for this, but that might also be overkill for a single file and you will lose anything since your last backup.

This article is not for beginners and assumes some knowledge of WordPress development and technologies such as sFTP.

The Solution

Media Sync is an excellent WordPress plugin that solves this very problem. At time of writing, the plugin is being actively maintained and it has great reviews in the official theme repository.

Before we get started, this requires that you have a backup of the file.

Some background information

Media library files are stored in the wp-content/uploads directory, from here it has a file for the year and month of upload e.g. wp-content/uploads/2018/04/ would be all files uploaded in April 2018.

Get Started

  • Install the Media Sync plugin from within WordPress. From the WordPress admin go to Plugins >> Add New – search for Media Sync.
  • Once installed, you’ll have a new option under the “Media” menu.
Media Sync Menu
  • Upload your missing file to the original location. You can do this using sFTP or SSH. To find out the original location, find a page with the image on and use text view in the classic editor or code editor in Gutenberg, this will have the original image URL still in it.
  • Once uploaded, the image is now ‘fixed’ it will display again on the front-end, but it will not appear in the media library. Here’s where Media Sync comes in to play.
  • Go to Media >> Media Sync.
  • This screen has two options:
    • Sync – uploads directory. This allows you to see content from your /wp-content/uploads directory and import it in to the media library (this is the option that we need for our scenario.
    • Sync – Media Library. This shows items in the media library that are missing their associated files.
  • Select “Sync – uploads directory”.
  • Before proceeding, make a backup.
Media Sync - Uploads Directory
  • In the scenario of just uploading one or a few files, then you’re best off using the directory structure to manually select the files. Once selected click ‘import selected’.
  • Keep the ‘Dry Run’ checkbox checked at first as this shows what the results will be before commiting. If this is positive then uncheck this and run the import again.
  • The files should now appear in your media libary.

This same method can be used for whole directories, but be warned that the more files you add, the slower it will be. Low quality hosts may even time out whilst doing this, so it’s best to be as specific as possible, even if it does take a little more time.

Tidy up

Whilst the plugin is excellent, it’s unlikely that you’ll need to use it very often. For this reason we would recommend only installing it when you use it, and removing after use. Even the best made plugins can have vulnerabilities, and if it’s not being used then it’s one less to update.

Alternatives

A new plugin has come along which looks like a good alternative, so if Media Sync doesn’t work for you then WP Media Recovery is certainly worth trying.

Wrapping it up

We hope you’ve found this post useful – it’s an annoying problem that is solved very elegantly by the Media Sync plugin. Let us know how you get on in the comments below.