Getting Started

How to Delete All External Images from Your Database

Published

External Images creates WordPress attachment posts with a postmeta entry of meta_key = 'ei' and meta_value = 1. To fully remove all external images from your database, run the following SQL queries in order.

Back up first

Back up your database before running any of these queries. They delete rows directly and there is no undo. If you are on shared hosting, take an export from your host’s control panel or via wp db export before you start.

Step 1: Delete term relationships for external image attachments

This removes any taxonomy term associations (categories, tags, etc.) linked to external image attachment posts.

DELETE tr FROM wp_term_relationships tr
INNER JOIN wp_postmeta pm ON tr.object_id = pm.post_id
WHERE pm.meta_key = 'ei' AND pm.meta_value = 1;

Step 2: Delete the external image attachment posts

This removes the attachment posts themselves from wp_posts.

DELETE p FROM wp_posts p
INNER JOIN wp_postmeta pm ON p.ID = pm.post_id
WHERE pm.meta_key = 'ei' AND pm.meta_value = 1;

Step 3: Delete all postmeta for external image attachments

Now that the posts are gone, clean up all postmeta rows that belonged to those same attachments, including the ei entry itself. The self-join resolves before deletions occur, so this is safe in a single query.

DELETE pm FROM wp_postmeta pm
INNER JOIN wp_postmeta ei ON pm.post_id = ei.post_id AND ei.meta_key = 'ei' AND ei.meta_value = 1;

Notes

  • Replace wp_ with your actual table prefix if it differs.
  • After running all queries you may want to go to Dashboard > External Images and verify that the image count is zero.
Performance Tip

On a large store the wp_postmeta table can hold millions of rows, and a single unbounded DELETE can lock the table and time out. If you have hundreds of thousands of external images, add a LIMIT (for example LIMIT 5000) to each query and run it repeatedly until it affects zero rows, or run the deletions during a quiet traffic window. Keeping wp_postmeta lean is one of the biggest wins for query performance at scale.

Did you know?

The whole point of External Images is that these attachment rows are lightweight pointers, not downloaded copies. Deleting them removes the database records but never touches the original images on their remote host, so nothing is lost from the source.

Further reading

what the `ei` postmeta and fake attachment posts actually do
if the count does not drop to zero after these queries

Comments

▲
▼
1/1