Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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.

About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Blackhole Pro: Trap bad bots in a virtual black hole.

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
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.