The WordPress error “Could not insert attachment into the database” means that WordPress could not create the attachment record it needs for a media upload. Check the exact error, storage availability and database logs first. Avoid repairing tables, changing database structure or overwriting files until you have identified the underlying problem.
A file can reach your server even if it never appears in the Media Library. WordPress needs both the uploaded file and its associated database record. Thumbnail generation is another stage. Knowing where the process fails helps you avoid unrelated fixes, such as rebuilding the whole site because a single image will not upload.

Find the stage that failed
| Symptom | Likely stage to investigate |
|---|---|
| Upload rejected before transfer | File type, size, permissions or request restrictions |
| File exists but attachment record is missing | Database insertion and server logs |
| Attachment exists but preview is broken | File URL, storage or attachment metadata |
| Only larger images fail | Upload limits, processing memory or timeouts |
| All database writes fail | Database availability, quota or permissions |
1. Understand how an attachment enters the Media Library
WordPress stores media as an attachment post, using the installation’s posts table and related metadata. The original file is normally stored in the uploads directory or a configured external storage service. The official developer reference for wp_insert_attachment() explains that it delegates attachment creation to wp_insert_post(). In the relevant core insertion failure branch, WordPress can return the message about being unable to insert an attachment into the database.
A successful file transfer therefore does not establish that the complete upload succeeded. Conversely, a missing thumbnail does not establish that database insertion failed. The thumbnail may be created later or fail during image processing. When you contact support, identify which stage produced the error, whether the Media Library contains a record and whether the original file exists. These details narrow the diagnosis much more effectively than a general claim that the database is corrupt.
2. Reproduce the problem with a small, supported image
Sign in using an authorised account with media upload permissions. Try uploading a small, ordinary JPEG or PNG that you know opens correctly on your device. Give it a simple filename and note the time of the attempt. If it succeeds, compare its size, format and dimensions with the original failing image. Try an independent small image before concluding that a particular file is defective.
If every supported file produces the same message, the issue may be broader than one image. If only a large photograph fails, check the upload and processing limits rather than assuming the database is damaged. Test whether the file appears in Media Library after a refresh; sometimes a request reports an error even though an earlier stage succeeded. Do not retry the same large file dozens of times because incomplete uploads may accumulate.
Keep the error message and a screenshot without revealing private filenames or account credentials.
3. Check storage limits and ask the host for evidence
Open your hosting dashboard and review available disk space, file-count limits and database quotas. A website may have enough space for an image but insufficient resources for a new database write. Ask the host whether the database server is healthy and whether your account can insert normal records. Provide the timestamp, affected domain and exact error. A qualified administrator can inspect the relevant application and database error logs.
A quota issue, database permission problem, invalid data or damaged table requires a different remedy. Do not increase every PHP setting or alter the database account blindly. If the host reports that storage is exhausted, remove or archive only files you have confirmed are unnecessary and have permission to manage. Preserve recent backups and uploaded content. Never delete database files directly from the server to reclaim space.
After an authorised correction, retry a single small upload and record the result.
4. Check the uploaded file and attachment record separately
Use the Media Library’s list or grid view to search for the filename. If the attachment appears, open its details and inspect the displayed file URL and image preview. The presence of the record suggests the insertion stage succeeded, even if another stage reported an error. If the image does not appear, an authorised administrator can check whether the original file was saved in the configured storage location.
Do not assume every installation uses the default uploads path, because plugins and external storage can change the workflow. An orphaned file can exist without an associated attachment record. Before deleting it, determine whether another post or process references the file. Preserve a copy when uncertain. A healthy attachment record should refer to the intended file, and the public URL should resolve through the site’s expected storage arrangement.
If files exist but records do not, concentrate on the insertion error and evidence from the logs.
5. Investigate database issues without guessing
A database insertion error does not prove that a table needs repair. Ask your hosting provider or database administrator to identify the underlying error before changing tables. For example, a read-only database, quota exhaustion or a malformed value would not be corrected simply by optimising a table. The posts table may have a custom prefix, and production installations can have additional database configuration. Do not recreate primary keys, reset auto-increment values, convert collations or import a generic SQL schema based only on an error message.
Such changes may destroy information or create conflicts with existing records. If a qualified administrator confirms corruption or another structural problem, first create and verify an appropriate recovery copy, then follow the remedy for that database engine and condition. WordPress has a documented database repair facility, but it is not the default solution for every insertion failure. Any temporary repair setting should be removed when the authorised work is complete.
See our WordPress backup guide before attempting database maintenance.
6. Investigate plugins and migrations in an isolated environment
If the issue began after an update or hosting move, record the exact change and compare the current installation with the previous working setup. Media plugins can alter file handling, metadata and storage; security tools can restrict particular upload requests. Do not disable every protection on the production website to test a theory. Create a staging copy where possible, reproduce the failure there and isolate one relevant component at a time.
Test with the same image and account permissions. If the problem disappears after disabling a specific extension on staging, inspect the vendor documentation and check for a compatible update. A clean installation on the same host may help distinguish site-specific settings from account-wide issues, but a successful test does not prove that every server setting is correct for production. Differences in resources, database permissions or storage configuration can remain.
Compare the relevant settings with support rather than copying staging configuration indiscriminately.
7. Separate image processing and request limits from database writes
WordPress and its server environment may restrict the size of an upload or the time available to process it. A large image can require considerably more working memory than its compressed file size suggests. If only large images or certain formats fail, test a smaller copy and check the host’s documented limits. A failure during thumbnail generation can leave the original file and attachment record intact.
In that situation, investigate the supported image processing library and metadata-generation process instead of repairing the database. If a request returns an access error, ask whether the user role, security rules or request limits are responsible. Check the response and error logs before changing a firewall. Keep protection enabled unless a controlled test identifies a specific configuration problem. Once the cause is known, change the smallest relevant setting and repeat the same upload test.
If you develop templates that display media, our attachment image function guide explains the separate display stage.
8. For developers: use supported WordPress media APIs
Custom upload code should use WordPress’s supported APIs rather than writing attachment rows directly with SQL. The developer handbook documents media_handle_upload() for supported upload workflows, and wp_insert_attachment() for creating attachment records when a file is already available. Request error objects where supported and check the returned value before generating metadata. Do not pass a failed insertion result to functions that expect a valid attachment ID.
The correct workflow also depends on whether the code runs in an administration screen, a REST route or another controlled application context. Validate the user’s capability, the request and the uploaded file using the appropriate WordPress facilities. Preserve error details in a protected log rather than displaying database messages or paths to public visitors. If a media-generation step fails after insertion, handle the partial result deliberately instead of creating another attachment on every retry.
Test the custom code in a disposable development environment with supported and rejected file types.
9. Verify the repair and monitor future uploads
Upload a small test image through the normal Media Library interface. Confirm that the attachment record appears, the original file opens and the expected thumbnail sizes are available. Add the image to a private test draft and preview it. Check that the original failing file now works if its format and size are supported. Review the relevant logs after the successful test and confirm that the same insertion error is no longer appearing.
Clean up only the test uploads you created and keep the site’s existing media untouched. Record the exact correction, including any hosting changes, so you can investigate a recurrence. Monitor quotas and establish dependable offsite backups. When migrating or updating the website, include a media upload in your acceptance checks. Our WordPress security guide explains why supported software and controlled changes reduce avoidable problems.
Frequently asked questions
Does this error mean the database is corrupted?
No. It identifies a failed attachment insertion. Logs and controlled tests are needed to establish the actual cause.
Can I repair the database without losing content?
Do not assume that every repair is risk-free. Back up the current state, identify the database error and use the remedy appropriate to that condition.
Why does my image exist on the server but not in Media Library?
The file transfer may have succeeded while creation of the attachment record failed. Investigate the two stages separately.
Start with one reproducible upload, check the exact failure stage and obtain the relevant logs. Make only a correction supported by the evidence. You can restore normal uploads without risking the entire database through speculative repairs.





