On this page

    M

    process.finalization.unregister

    History
    process.finalization.unregister(ref): void
    Stability: 1.1Active Development
    Attributes
    The reference to the resource that was registered previously.

    This function remove the register of the object from the finalization registry, so the callback will not be called anymore.

    const { finalization } = require('node:process');
    
    // Please make sure that the function passed to finalization.register()
    // does not create a closure around unnecessary objects.
    function onFinalize(obj, event) {
      // You can do whatever you want with the object
      obj.dispose();
    }
    
    function setup() {
      // This object can be safely garbage collected,
      // and the resulting shutdown function will not be called.
      // There are no leaks.
      const myDisposableObject = {
        dispose() {
          // Free your resources synchronously
        },
      };
    
      finalization.register(myDisposableObject, onFinalize);
    
      // Do something
    
      myDisposableObject.dispose();
      finalization.unregister(myDisposableObject);
    }
    
    setup();
    import { finalization } from 'node:process';
    
    // Please make sure that the function passed to finalization.register()
    // does not create a closure around unnecessary objects.
    function onFinalize(obj, event) {
      // You can do whatever you want with the object
      obj.dispose();
    }
    
    function setup() {
      // This object can be safely garbage collected,
      // and the resulting shutdown function will not be called.
      // There are no leaks.
      const myDisposableObject = {
        dispose() {
          // Free your resources synchronously
        },
      };
    
      // Please make sure that the function passed to finalization.register()
      // does not create a closure around unnecessary objects.
      function onFinalize(obj, event) {
        // You can do whatever you want with the object
        obj.dispose();
      }
    
      finalization.register(myDisposableObject, onFinalize);
    
      // Do something
    
      myDisposableObject.dispose();
      finalization.unregister(myDisposableObject);
    }
    
    setup();