Save 15% on our Pro WordPress plugins with discount code: LAUNCH2021
Web Dev + WordPress + Security

Delete Shared/Saved Gutenberg Blocks

Been playing with WordPress new Gutenberg functionality. While exploring the new features, I created some Shared blocks via the “Convert to Shared Block” button. After another hour of playing with the Gutenberg API, there were a number of “orphaned” Shared blocks (just due to swapping out code snippets while testing). After some searching, I found it is possible to delete Shared blocks programmatically with JavaScript, but could not find any specific documentation or examples. So, I came up with an alternate way to delete Shared blocks via the CPT UI in the WP Admin Area.

To save time, I sometimes abbreviate Gutenberg as a numeronym: G7G or g7g.

Delete Shared/Saved Gutenberg Blocks

This technique does not require JavaScript. Instead, it works like this:

  1. Add a simple code snippet via your theme’s functions.php
  2. Visit the WP Admin Area > Blocks, and delete any unwanted blocks

So you can remove any unwanted or orphaned “Shared” or “Saved” Gutenberg blocks.

Step 1: Add code snippet

Here is the code to add to functions.php:

// remove this code when finished!
function g7g_modify_wp_block() {
	
	global $wp_post_types;
	
	$cpt = 'wp_block';
	
	if (empty($wp_post_types[$cpt]) || !is_object($wp_post_types[$cpt]) || empty($wp_post_types[$cpt]->labels)) return;
	
	$wp_post_types[$cpt]->show_ui      = true;
	$wp_post_types[$cpt]->show_in_menu = true;
	$wp_post_types[$cpt]->_edit_link   = 'post.php?post=%d';
	
}
add_action('init', 'g7g_modify_wp_block', 1111);

What this code is doing? It is modifying the Custom Post Type used by Gutenberg blocks, wp_block. By default, the wp_block CPT does not enable the admin UI. So we grab the global post-types variable and modify to enable the admin interface.

Note: this technique is meant to be temporary, and removed after you’ve deleted any unwanted blocks. Make sure to remove the code when finished, and never enable on a live, production site. I.e., it’s just for testing and experimenting, while learning Gutenberg.

Step 2: Visit the Blocks UI

After adding the code from Step 1, you can visit the WP Admin Area to find a new menu item for the “Blocks” post type. So take a visit to find all Shared/Saved blocks, ready for deleting. Just like any other post type.

Remember to remove the code snippet when no longer needed.

Disclaimer

I am just learning Gutenberg. And I am not a JavaScript wizard. If you know of a better/easier way of deleting Shared blocks, please share via the comments below. Hopefully the Gutenberg developers will add a button or something to delete unwanted blocks in a future update. Until then, this technique works well.

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Banhammer: Protect your WordPress site against threats.

One response to “Delete Shared/Saved Gutenberg Blocks”

  1. Thank you so much for sharing this! I have been experimenting with the Gutenberg plugin on a couple of live sites the past few months.

    Was thinking about possibly using shared blocks to custom insert ad placements – but worried if in the long term it would cause issues if I ever needed to remove the blocks later down the road – definitely would not want to delete all those blocks on a post-by-post manual basis!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Thoughts
WP 5.8 Gutenberg/Block Widgets is breaking many sites. Fortunately Disable Gutenberg makes it easy to restore Classic Widgets with a click.
Easily the most common exploit scan for WordPress is /{path}/wp-login.php.
Pushing 110+ ℉ for several days now, expected for at least another week or so.
After 12 intense weeks the Plugin Planet redesign is now live. Much work still happening behind the scenes.
June, July, August historically are slow months on the Web. Perfect time to get some real work done (think projects).
Redesigning Plugin Planet is one the most challenging things I’ve done online. Almost there, about another two weeks ’til launch.
I could listen to Mouse Rat all day.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.