Sample SharePoint Framework application customizer showing how you can run code once, and then remove the customizer at the end. The code as-is expects to be installed as a site scoped custom action.
Open this sample in the gallery (popup panel): Open in gallery
Source: https://github.com/pnp/sp-dev-fx-extensions/tree/main/samples/js-application-run-once