title: How do I delete the old Custom Post Type permalink after rewriting the URL to exclude the slug?
body: <p>I have a <code>Custom Post Type</code> called Book, and the link is: <code>mywebsite.com/book/mybookname</code></p>
  
  <p>I want to change this so that the link is <code>mywebsite.com/mybookname</code>.</p>
  
  <p>I have added the following code to change the link and it works as expected:</p>
  
  <pre><code>function books_theme_remove_slug( $post_link, $post, $leavename ) {
  
      if ( 'book' != $post-&gt;post_type || 'publish' != $post-&gt;post_status ) {
          return $post_link;
      }
  
      $post_link = str_replace( '/' . $post-&gt;post_type . '/', '/', $post_link );
  
      return $post_link;
  }
  add_filter( 'post_type_link', 'books_theme_remove_slug', 10, 3 );
  
  function books_theme_parse_request( $query ) {
  
      if ( ! $query-&gt;is_main_query() || 2 != count( $query-&gt;query ) || ! isset( $query-&gt;query['page'] ) ) {
          return;
      }
  
      if ( ! empty( $query-&gt;query['name'] ) ) {
          $query-&gt;set( 'post_type', array( 'post', 'book', 'page' ) );
      }
  }
  add_action( 'pre_get_posts', 'books_theme_parse_request' );
  </code></pre>
  
  <p>The problem is that the old link(<code>mywebsite.com/book/mybookname</code>) is still working. I'd like to make that link go to a 404 page without breaking the current links.</p>
  
  <p>I tried the following but it breaks everything: </p>
  
  <pre><code>function books_theme_parse_request( $query ) {
      if(isset($query-&gt;query['post_type']) &amp;&amp; $query-&gt;query['post_type'] == 'book'){
          global $wp_query;
          $wp_query-&gt;set_404();
          status_header( 404 );
          get_template_part( 404 ); exit();
      }
  
      if ( ! $query-&gt;is_main_query() || 2 != count( $query-&gt;query ) || ! isset( $query-&gt;query['page'] ) ) {
          return;
      }
  
      if ( ! empty( $query-&gt;query['name'] ) ) {
          $query-&gt;set( 'post_type', array( 'post', 'book', 'page' ) );
      }
  }
  add_action( 'pre_get_posts', 'books_theme_parse_request' );
  </code></pre>
  
  <p>How do I delete old url?</p>