Reset a facet on first page load if it has a selection in the URL

<?php
// Resets a facet on first page load only, if it has a selection in the URL. 
// Can be used if users have bookmarked links with now non-existing facet choices.
// Retains other facet choices.

// Part 1: Reset the facet choice by preloading the empty url vars for it on first page load
add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
  if ( 'demo/cars' == FWP()->helper->get_uri() ) { // Replace 'demo/cars' with the URI of your page (everything after the domain name, excluding any slashes at the beginning and end)
    if ( ! empty( $url_vars['myfacetname'] ) ) { // Replace 'myfacetname' with the name of your facet
      $url_vars['myfacetname'] = []; // Replace 'myfacetname' with the name of your facet
    }
  }
  return $url_vars;
} );

// Part 2: Remove the facet's URL vars for the facet
add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if ( ! FWP.loaded ) { // Only on first page load
        var facet_name = 'myfacetname';
        if ( 'object' == typeof FWP.facets[facet_name] && FWP.facets[facet_name].length  ) {
          FWP.facets[facet_name] = []; // Remove choices
          FWP.setHash(); // Reset the URL vars
        }
      }
    });
  </script>
  <?php
}, 100 );