New Bookstore! Save 20% on books with discount code: LAUNCH
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 = Creative thinker. Passionate about free and open Web.
The Tao of WordPress: Master the art of WordPress.

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 »
Banhammer: Protect your WordPress site against threats.
Thoughts
LOL @ “weekly” support: “Your request has been received and will be reviewed soon. Neil (your support guy) checks support requests weekly. Rest assured we’ll get to it!” — Gotta be kidding with this.
After several days of work, finally taking a break. If anyone needs me, I'll be in Zora’s Domain, working thru Mah Eliya shrine.
Never ever call the client a useless idiot. Even if it’s true.
Inevitable that Google shuts down Feedburner. Maybe this year? Who knows. Here is a nice summary plus alternatives.
There is no way that Marvel can serve up anything better than what they've already done with Infinity saga/Endgame. Knowhere to go but down, sadly.
I built a new search launchpad to make my life easier. So I can search multiple engines quickly and easily.
Updating all muh plugins for WP 5.5 imminent release! Wut August :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.