Welcome to the new design! Please report any bugs or issues, thanks :)
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 = Web Developer. Book Author. Secretly Important.
BBQ Pro: The fastest firewall to protect your 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 »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
This new Admin Page Framework looks pretty good. Can't wait to check it out.
Two great places to find awesome plugins: pluginsearch.com and WP's browse new.
2 things I hate to see in stylesheets: _ and #
Love VLC media player but it fails miserably when it comes to randomizing large collections of mp3 and other files.
Dashlane redesigned, stating proudly they "removed all filigree". Should have kept it; the app now looks generic and boring. Killed your identity.
Working on integration for setaPDF + EDD on the new books subdomain. Good times.
Toggle visibility of hidden files on Mac: Cmd + Shift + .