Drupal change filename of an uploaded image

Question:
How to change the filename of an image on Drupal?

We want to change the image filename:

pexels-photo-721996.jpeg

to:

fairground-carousel.jpeg
Image:
MySQL with Drupal filename to be changed
Drupal table 'file_managed' we locate our file for which we want to change the filename

Under the column 'uri' of the table 'file_manged' we can see the location of the file is:

public://2022-11/pexels-photo-721996.jpeg

As such within our directory 'web/sites/default/files/2022-11'  we need to change the filename to the new desired filename.

Within Drupal the filename is visible under the files tab.

Image:
Drupal filename that we want to change

Steps

1. Change filename in the Drupal database table 'file_managed'.

Image:
MySQL edit

 

Image:
MySQL edit fields

2. Clear the cache.  You'll then see within Drupal as follows under 'Files' and 'Media' assuming the image was uploaded via media.

Files

Image:
Updated Drupal filename

Media

Image:
Updated filename under Media

In both cases the thumbnail is missing as we have not yet updated the filename.  This has to be done on the server.

After the filename has been updated the thumbnails will once again appear.

Note, if you're wanting to change the media name which is separate to the filename then the tables that need updating are:

  • media_field_data
  • media_field_revision

And that's how to change the name of an image file that's already been uploaded into Drupal.