You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cropping image on create fails with "Failed to persist upload data" #15991
Uploading an image and applying a crop during initial document creation fails to save the S3 URL/metadata back to the document. The server logs a warning: Failed to persist upload data for collection <slug> document <id>. The file itself is uploaded to S3 successfully — only the metadata persistence step fails.
This does not happen when uploading without a crop, or when cropping an already-saved document.
Please add a reproduction in order for us to be able to investigate.
Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.
Why was this issue marked with the invalid-reproduction label?
To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with create-payload-app@latest -t blank or a forked/branched version of this repository with tests added (more info in the reproduction-guide).
To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.
Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.
I added a link, why was it still marked?
Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.
I hit a different symptom of what I believe is the same root cause, and traced it through the source. The bug is that the cloud-storage plugin's internal metadata update re-enters Payload's file re-processing path because req.query.uploadEdits is never cleared.
Root cause
After an upload, plugin-cloud-storage's afterChange hook persists upload metadata via an internal req.payload.update() that reuses the same req:
Before that call it sets req.context.skipCloudStorage = true and clears req.file and req.payloadUploadSizes ("Clear to prevent re-processing") — but it does not clear req.query.uploadEdits.
Meanwhile, core's generateFileData reads uploadEdits straight from req.query on every update (parseUploadEditsFromReqOrIncomingData in packages/payload/src/uploads/generateFileData.ts), and shouldReupload() returns true whenever it contains a crop/resize/focal change. So the plugin's supposedly data-only metadata update re-enters the full file-regeneration path: with req.file cleared, generateFileData tries to re-fetch the file (getExternalFile / getFileByPath) and re-run sharp with the crop applied a second time.
Symptoms
This issue (create + crop): the re-fetch on the internal update pass fails (e.g. the doc's url isn't fetchable from the server at that moment), the error is caught in afterChange, and you get Failed to persist upload data... with the URL/metadata never written.
My case (update + crop, custom beforeOperation hook that populates req.file): the internal update pass re-runs sharp, getSafeFileName() bumps the filename another generation, but since skipCloudStorage makes the plugin skip its uploads, the DB filename ends up one generation ahead of the objects actually stored in S3 — and the next image edit 404s.
Both fall out of the same thing: the stale uploadEdits in req.query makes the metadata-persistence update look like a fresh image edit.
Suggested fix
In afterChange.ts, alongside clearing req.file and req.payloadUploadSizes, also strip uploadEdits from req.query (or pass a req without the query) before the internal update(). Alternatively, core could ignore req.query.uploadEdits when req.context.skipCloudStorage is set, but sanitizing the req in the plugin seems cleaner since it owns the internal update.
Workaround
A beforeOperation hook on the upload collection that strips the stale edits on the plugin's internal pass:
We are hitting what looks like exactly this bug, and can offer a full reproduction plus the mechanism, since the issue is waiting on one. Two updates from current versions:
On payload / plugin-cloud-storage 3.85.2 the symptom has escalated: the afterChange hook's catch block now rethrows, so instead of the logged "Failed to persist upload data" warning the whole save fails with HTTP 400 — "There was a problem while uploading the file". Any upload saved with a crop fails on a cloud-storage install (admin UI or REST); focal-point-only edits survive.
Verified against packages/plugin-cloud-storage/src/hooks/afterChange.ts on current main: the cleanup gap described below is still there.
Environment
Package
Version
payload
3.85.2
@payloadcms/plugin-cloud-storage
3.85.2
@payloadcms/storage-s3
3.85.2
@payloadcms/db-postgres
3.85.2
next
16.2.12
sharp
0.34.2
Node
22.23.2
Reproduction
An upload collection with crop: true (default) and some imageSizes, storage via @payloadcms/storage-s3.
In the admin UI: upload an image, open the crop tool, apply any crop, save. Equivalently via REST — multipart file body plus:
POST /api/images?uploadEdits[crop][unit]=%25&uploadEdits[crop][x]=0&uploadEdits[crop][y]=10.47&uploadEdits[crop][width]=91.89&uploadEdits[crop][height]=42.9&uploadEdits[focalPoint][x]=24&uploadEdits[focalPoint][y]=31&uploadEdits[heightInPixels]=644&uploadEdits[widthInPixels]=1103
The same request without the crop params succeeds.
Error output
BadRequestError: There was a problem while uploading the file.
Caused by: Error: extract_area: bad extract area
at cropImage (payload/dist/uploads/cropImage.js:49)
at generateFileData (payload/dist/uploads/generateFileData.js:201)
at updateByIDOperation (...) ← the plugin's nested metadata update
at getAfterChangeHook (@payloadcms/plugin-cloud-storage/dist/hooks/afterChange.js)
at createOperation (...) ← the original request
Mechanism
The original create/update carries req.query.uploadEdits with a crop. generateFileData crops the file correctly — the first pass is fine.
plugin-cloud-storage's afterChange hook uploads the files to the adapter and then persists the adapter's upload metadata via a nested req.payload.update({ id, data: uploadMetadata, req })reusing the same req. Before that call it sets req.context.skipCloudStorage = true and clears req.file and req.payloadUploadSizes ("Clear to prevent re-processing") — but it does not clear req.query.uploadEdits.
The nested update re-enters generateFileData: parseUploadEditsFromReqOrIncomingData picks up the leaked req.query.uploadEdits, shouldReupload returns true because uploadEdits.crop is set, and cropImage applies the original pixel rectangle (widthInPixels/heightInPixels describe the original file) to the already-cropped file → extract_area: bad extract area.
The rethrow in the hook's catch turns that into a 400 for the whole save.
This matches @sam9191's earlier analysis in this thread; focal-only edits pass because the parser bails when the incoming focal point equals the stored one — which is why the bug only bites when a crop is involved.
Suggested fix
In getAfterChangeHook, alongside the existing req.file = undefined / req.payloadUploadSizes = undefined, drop the already-consumed edits before the nested update:
By the time skipCloudStorage is set the original request has fully consumed the edits, so deleting them there is safe. With this hook in place, create-with-crop returns 201 with a correctly single-cropped file on our install.
Activity
github-actions commentedon Mar 18, 2026
Please add a reproduction in order for us to be able to investigate.
Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.
Why was this issue marked with the
invalid-reproductionlabel?To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with
create-payload-app@latest -t blankor a forked/branched version of this repository with tests added (more info in the reproduction-guide).To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.
Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.
I added a link, why was it still marked?
Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.
Useful Resources
maiklukasch commentedon Apr 24, 2026
I have the same bug. Cropping when creating an image and image record doesnt get stored into the images-table
sam9191 commentedon Jul 18, 2026
I hit a different symptom of what I believe is the same root cause, and traced it through the source. The bug is that the cloud-storage plugin's internal metadata update re-enters Payload's file re-processing path because
req.query.uploadEditsis never cleared.Root cause
After an upload,
plugin-cloud-storage'safterChangehook persists upload metadata via an internalreq.payload.update()that reuses the samereq:https://github.com/payloadcms/payload/blob/main/packages/plugin-cloud-storage/src/hooks/afterChange.ts
Before that call it sets
req.context.skipCloudStorage = trueand clearsreq.fileandreq.payloadUploadSizes("Clear to prevent re-processing") — but it does not clearreq.query.uploadEdits.Meanwhile, core's
generateFileDatareadsuploadEditsstraight fromreq.queryon every update (parseUploadEditsFromReqOrIncomingDatainpackages/payload/src/uploads/generateFileData.ts), andshouldReupload()returnstruewhenever it contains a crop/resize/focal change. So the plugin's supposedly data-only metadata update re-enters the full file-regeneration path: withreq.filecleared,generateFileDatatries to re-fetch the file (getExternalFile/getFileByPath) and re-run sharp with the crop applied a second time.Symptoms
urlisn't fetchable from the server at that moment), the error is caught inafterChange, and you getFailed to persist upload data...with the URL/metadata never written.beforeOperationhook that populatesreq.file): the internal update pass re-runs sharp,getSafeFileName()bumps the filename another generation, but sinceskipCloudStoragemakes the plugin skip its uploads, the DB filename ends up one generation ahead of the objects actually stored in S3 — and the next image edit 404s.Both fall out of the same thing: the stale
uploadEditsinreq.querymakes the metadata-persistence update look like a fresh image edit.Suggested fix
In
afterChange.ts, alongside clearingreq.fileandreq.payloadUploadSizes, also stripuploadEditsfromreq.query(or pass areqwithout the query) before the internalupdate(). Alternatively, core could ignorereq.query.uploadEditswhenreq.context.skipCloudStorageis set, but sanitizing the req in the plugin seems cleaner since it owns the internal update.Workaround
A
beforeOperationhook on the upload collection that strips the stale edits on the plugin's internal pass:Reproduced on 3.80.0; the relevant code is unchanged on current
main.umkasanki commentedon Aug 6, 2026
We are hitting what looks like exactly this bug, and can offer a full reproduction plus the mechanism, since the issue is waiting on one. Two updates from current versions:
afterChangehook's catch block now rethrows, so instead of the logged "Failed to persist upload data" warning the whole save fails with HTTP 400 — "There was a problem while uploading the file". Any upload saved with a crop fails on a cloud-storage install (admin UI or REST); focal-point-only edits survive.packages/plugin-cloud-storage/src/hooks/afterChange.tson currentmain: the cleanup gap described below is still there.Environment
Reproduction
crop: true(default) and someimageSizes, storage via@payloadcms/storage-s3.Error output
Mechanism
req.query.uploadEditswith acrop.generateFileDatacrops the file correctly — the first pass is fine.plugin-cloud-storage'safterChangehook uploads the files to the adapter and then persists the adapter's upload metadata via a nestedreq.payload.update({ id, data: uploadMetadata, req })reusing the samereq. Before that call it setsreq.context.skipCloudStorage = trueand clearsreq.fileandreq.payloadUploadSizes("Clear to prevent re-processing") — but it does not clearreq.query.uploadEdits.generateFileData:parseUploadEditsFromReqOrIncomingDatapicks up the leakedreq.query.uploadEdits,shouldReuploadreturns true becauseuploadEdits.cropis set, andcropImageapplies the original pixel rectangle (widthInPixels/heightInPixelsdescribe the original file) to the already-cropped file →extract_area: bad extract area.This matches @sam9191's earlier analysis in this thread; focal-only edits pass because the parser bails when the incoming focal point equals the stored one — which is why the bug only bites when a crop is involved.
Suggested fix
In
getAfterChangeHook, alongside the existingreq.file = undefined/req.payloadUploadSizes = undefined, drop the already-consumed edits before the nested update:Or, more defensively, have
parseUploadEditsFromReqOrIncomingDataignore queryuploadEditswhen there is noreq.fileto process.Userland workaround
A
beforeOperationhook on the upload collection, keyed off the plugin's own marker for its internal update:By the time
skipCloudStorageis set the original request has fully consumed the edits, so deleting them there is safe. With this hook in place, create-with-crop returns 201 with a correctly single-cropped file on our install.