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 = Creative thinker. Passionate about free and open Web.
Digging Into WordPress: Take your WordPress skills to the next level.

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
Playing the long game.
They have weaponized the idiots.
Good software never steals focus from the user. Even during startup.
After 10 years running my own business, I still manage schedules and tasks using old school post-it notes, sometimes simple sometimes very elaborate.
You know those sites, where you're trying to just grab a quick bit of information but the page is shifting all over the place as it loads up 3 million advertisements.
Selling two of my top WordPress domains, wp-zen.com & zen-wp.com $300 for both. Aged 9 years. Drop a line if interested.
Never force your users to type out a password (or any long string of characters) by blocking the paste function. Typing long strings leads to MORE errors than simple copy/paste.