Regenerate thumbnail duplication webp file
Bug Description
If using the Regenerate Thumbnails plugin regenerate thumbnail cause file duplication.
Steps to reproduce
- Enable WebP Uploads
- Install Regenerate Thumbnails
- Upload the image
- Click Regenerate Thumbnails in the media library
- Image file duplicated
Screenshots
Before regenerate:

After regenerate:

Additional Context
- PHP Version: 8.1.12
- Plugin Version: 1.6.0
@james58899 When you ran into this bug, did you have the checkbox to generate both WebP and JPEG (under Settings > Media) enabled or disabled?
@james58899 When you ran into this bug, did you have the checkbox to generate both WebP and JPEG (under
Settings > Media) enabled or disabled?
I didn't enable it, only generate thumbnails in webp format.
@james58899 thanks for reporting the issue.
I replicate the issue when I regenerate the image using the plugin. The above PR resolves the issue with the full-size image. I find one more bug when I regenerate images multiple times.
Images after the first regeneration:

Images after the second regeneration:

@mukeshpanchal27 Actual issue is with replacing original image with custom mime type version here in this line: https://github.com/WordPress/performance/blob/trunk/modules/images/webp-uploads/load.php#L136
So it creates 2 issues there.
- Regenerating images: First regeneration has no issues because till this time, the original image is not replaced. After the first regeneration, metadata is updated. The original file is being replaced with custom mimed image. So second regeneration actually takes that replaced file instead of original uploaded file, that's why the sub sizes and full image have naming issues.
- Delete attachment: As per above scenario, the first regeneration replaces the original image with custom mimed image, metadata doesn't have any point of reference to the original uploaded file, so when we delete the attachment, that file remains in the file system.
Adding @adamsilverstein I added this to Images and the Needs Triage label. Please advise on next steps.
If using the Regenerate Thumbnails plugin regenerate thumbnail cause file duplication.
@james58899 - I assume you are using this plugin. With this plugin, unless you enable the "Delete thumbnail files" option, the old files are left in place when doing a regenerate command. Unless this box is checked WordPress creates new files and will generate new filenames as needed.
Closing this as "works for me" because this looks like the expected behavior to me. If I have missed something, please feel free to re-open this issue.
@adamsilverstein The issue is image-jpg.webp file regenerates as image-jpg-webp.webp and the original image replace as image-jpg.webp, which doesn't look like the expected behavior.
Regenerating the thumbnail should not change the information of the original image, nor should it infinitely generate new webp files.
Regenerating the thumbnail should not change the information of the original image, nor should it infinitely generate new webp files.
I will give this a test. The file naming probably isn't a concern as long as the file meta is right.
- Can you confirm if you have the "Delete thumbnails option" checked?
- Can you test without the plugin active to see what files are generated/is the behavior different?
@james58899 I am still unable to reproduce the issue, have you tested with the latest version of the plugin? is your original upload a jpeg? do you have a.custom filter applied to generate both jpeg and webp?
@adamsilverstein Yes, the issue still exists, please reopen the issue. According to the above comment, this issue is obviously reproducible, and there is a PR related to this issue, how did you try to reproduce this issue?
Enable Performance Lab
Settings

The picture was just uploaded
Everything works fine when the image is just uploaded.

First time regenerate thumbnail
After the thumbnail is regenerated for the first time, the original image is replaced with webp, and a new webp file is generated.

Second time regenerate thumbnail
After the thumbnails are regenerated a second time, the original images are replaced again, and a bunch of new webp thumbnails are generated.

Click Delete permanently
A lot of files are left in the folder without being deleted.

Disable Performance Lab
The picture was just uploaded

First time regenerate thumbnail

Second time regenerate thumbnail
Same as first time.
Click Delete permanently
All files have been deleted.

@james58899
I can confirm your findings.
Initially, I couldn't reproduce issue, until I disabled [_] Generate JPEG files in addition to WebP, like you did here.

@adamsilverstein
@adamsilverstein I'm going to reopen the issue as i still replicate the issue.
Steps to reproduce
-
Install and activate the Performance Lab plugin with default settings (Enable WebP Uploads)
-
Install Regenerate Thumbnails plugin
-
Upload the image. ex.
car.jpeg. -
Go to Media > Edit image then click on
Regenerate Thumbnailsbutton. -
Again click on
Regenerate Thumbnailsbutton.
Excellent, thank you @mukeshpanchal27 and @james58899 for the detailed steps to reproduce. I will retest and see what the best solution is. It looks like in an attempt to avoid name collisions we are inadvertently creating extra names.
I'm curious (and will test) - if you delete the original image, are you left with stray images that aren't deletes?
I can confirm the same behaviour when using .avif format.