How to clean a hacked WordPress database
Back up the database first, then strip injected rows from the options, posts, comments and user meta tables, and finally harden the site so the backdoor that caused the hack cannot survive. They do not tell you which tables to open, which rows to delete, or how to prove the backdoor is gone before you go live. This is the checklist for doing it yourself or briefing the person who will.
Key takeaways
- Cleaning the visible spam without finding the backdoor is why sites get reinfected.
- Do not delete active_plugins or theme unless you are certain the value is malicious.
- Do not request the re-crawl until you have confirmed the site is clean in an incognito window.
- Cleaning without hardening means the next exploit finds the same open door.
Back up the database before you touch anything
WordPress runs on a MySQL or MariaDB database [1]. Before you delete a single row, export the whole thing.
- Open your host's database tool (phpMyAdmin, Adminer, or the command line) and run a full export. If you have shell access,
mysqldump --single-transaction --routines --triggers your_db > backup_YYYYMMDD.sqlgives you a consistent snapshot. - Move the dump file off the server. Download it to your own machine. A live backdoor can reach anything inside the web root or on the same box, so a backup sitting in
/home/youruser/backups/is not safe. - Write down the exact timestamp of the dump. You will use it later to compare against the hack window and to confirm which rows were injected after the last clean backup.
If a clean breaks a table, you roll back to this dump. Without it, you are editing live with no way back.
Find the backdoor before you clean
Cleaning the visible spam without finding the backdoor is why sites get reinfected. The backdoor is the row or file that re-injects the spam after you delete it.

Search the database for these patterns:
- Base64-encoded strings in any text column.
eval(orpreg_replacecalls with the/eflag.- Unknown admin users in
wp_users.
Check the wp_options table specifically for a hijacked home or siteurl value, and for any option whose value points to a gambling or spam domain.
Once you have a suspicious row, open the file it references on the server and inspect its content. Look for the exact PHP payload, function call, or hash that appears in that database row. If the file contains the same code or identifier as the row, the backdoor is still present.
Strip the wp_options table
The wp_options table stores site-wide settings. Attackers hide redirects and scripts here because many standard scanners do not inspect option values deeply.
- Open
wp_optionsand sort byoption_valuelength, descending. Long, unreadable values are the first things to check. - Delete any option whose value contains a redirect, an
<iframe>, or a<script>tag you do not recognise. - Reset
homeandsiteurlto your correct domain if they were pointed at a spam site. - Remove any option that stores a hidden admin reference or a serialized payload you cannot explain.
Do not delete active_plugins or theme unless you are certain the value is malicious. A wrong deletion here breaks the site in a way that is harder to fix than the hack itself.
Clean the posts and comments tables
Injected spam often lives in post content, page content, or comments.
- In
wp_posts, searchpost_contentandpost_content_filteredfor base64 strings, hidden text (display:none,font-size:0), or spam links. Delete or strip those rows. - In
wp_comments, look for comments created in a burst that matches the hack window. Delete comments that carry spam links or that you do not recognise. - After cleaning, run a quick check: open a few posts in the front end and confirm no hidden text or redirect remains.
If you are unsure whether a post is legitimate, move it to the trash rather than deleting it. You can restore it later if you made a mistake.
Remove the rogue users and user meta
Attackers create admin accounts to keep access after you clean the visible spam.
- Open
wp_usersand list every account. Delete any admin or user you do not recognise. - In
wp_usermeta, clear the matching rows for the deleted users. Pay attention to rows that grant theadministratorrole to a stranger. - Reset the password of every remaining legitimate account. Force a re-login so any active session tokens tied to the old password are invalidated.
If you cannot identify which accounts are yours, check the email addresses in wp_users against your own records before deleting.
Request the Google review and restore trust
Once the redirects and warnings are gone, you need Google to re-crawl the site.

- Open Google Search Console and go to the Security report. Confirm the manual action or malware warning is still listed.
- In an incognito window, load your site and confirm the red browser warning and gambling redirects no longer appear.
- In Search Console, request a re-crawl. If a manual action is listed, submit a reconsideration request with a short description of what you cleaned.
Do not request the re-crawl until you have confirmed the site is clean in an incognito window. If the redirect is still live, Google will flag it again and the warning will persist.
Harden against reinfection
Cleaning without hardening means the next exploit finds the same open door.
- Update WordPress core, every plugin, and every theme to the latest version before you go live.
- Enable two-factor authentication for every admin account. Remove any unused plugin or theme.
- Set up a scheduled database backup and a file-integrity check so the next backdoor is caught before it redirects a visitor.
For a deeper walkthrough of the hardening steps, see How to harden WordPress against reinfection after a hack.
If you want the full checklist, the SQL queries for each table, and the Google reconsideration template in one document, the WordPress Hack Recovery Guide pack covers the steps above with copy-paste commands and a worked example.
Frequently asked questions
What should I do before deleting rows?
Back up the database first, then strip injected rows from the options, posts, comments and user meta tables, and finally harden the site so the backdoor that caused the hack cannot survive.
Sources
Related on this site
- How to back up a hacked WordPress site before you clean it
- How to tell if your WordPress site is actually hacked or just flagged
- How to recover a hacked WordPress site in 7 steps
- Website hack downtime cost calculator – free calculator
- WordPress Hack Recovery Checklist – the full guide
- WordPress hack recovery guide – every article on this topic
This article was drafted with AI assistance and checked before publication.
Free calculator: Website hack downtime cost calculator
The full guide: WordPress Hack Recovery Checklist
