Updating your file paths in MySQL

Have you ever had the problem when moving a site between environments, only to find that your file paths are now broken? It can also occur if you use a multisite setup and are trying to clone one of the existing ones.

Here's a MySQL snippet to help you update your images and other files to their new path. It goes without saying that you should backup your database before doing this. The old saying of backup, backup, backup...

UPDATE files SET filepath = REPLACE(filepath, 'sites/your-original-site/files', 'sites/your-new-site/files');

This will update all the file paths that start with 'sites/your-original-site/files' to 'sites/your-new-site/files'.  For instance 'sites/your-original-site/files/image.jpg' will now become 'sites/your-new-site/files/image.jpg'.

PS. remember to also change your file system paths if needed - Configuration > Media > File system (/admin/config/media/file-system)

Drupal version: 
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.