# Clear the Embed (oEmbed) Cache

> Run on demand to delete every cached embed (videos, tweets and other oEmbed results) from the database, so they are fetched fresh and stale or broken embeds disappear.

- Type: PHP
- Runs: On demand
- Category: [Performance](https://wpide.com/library/category/performance/)
- Tags: oembed, cache, database
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/perf-clear-oembed-cache/

## Code

```php
global $wpdb;

$wpide_meta = (int) $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '\_oembed\_%'" );
$wpide_ids  = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'oembed_cache' LIMIT 2000" );

foreach ( $wpide_ids as $wpide_id ) {
    wp_delete_post( (int) $wpide_id, true );
}

wp_cache_flush();

printf( 'Removed %d cached embed meta row(s) and %d oEmbed cache post(s).', $wpide_meta, count( $wpide_ids ) );
```

## Notes

Run it from the snippet editor; embeds are fetched again the next time each page is viewed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Clear the Embed (oEmbed) Cache" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
