FinalizationRegistry
The FinalizationRegistry global object lets a program register objects and non-registered symbols so that a cleanup callback can run at some point after one of them has been garbage-collected. Each registration pairs a target with a held value, and the callback receives the held value. The target itself is already gone by then:
> const registry = new FinalizationRegistry((name) => {
… console.log(`${name} was collected`);
… });
undefined
> registry.register({ rows: 5000 }, "report")
undefined
The registry holds each target weakly, so registering an object doesn’t keep it alive. It holds the held value strongly, since the callback still needs it after the target is gone. Here’s how the registry holds each part of a registration:
The engine decides when, and whether, a cleanup callback runs. It never runs synchronously during the code that drops the last reference. It might run much later, or not at all, for example when the program exits first. Registries suit secondary cleanup, such as removing stale cache entries.
FinalizationRegistry was added in ES2021 alongside WeakRef and is Baseline Widely available. Non-registered Symbol values became valid targets in ES2023. That support is Baseline Newly available since 2025, and works in Node.js 20 and later.
Syntax
The FinalizationRegistry constructor takes the cleanup callback:
new FinalizationRegistry(<cleanupCallback>)
| Parameter | Description |
|---|---|
cleanupCallback |
A required function that receives one argument, the held value of a registration whose target was collected |
The constructor returns a new FinalizationRegistry. The new keyword is required, and calling FinalizationRegistry() without it throws a TypeError.
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.register() |
Instance method | Registers a target with a held value and an optional unregister token, and returns undefined |
.unregister() |
Instance method | Removes every registration made with a given token and returns whether it removed any |
Without an unregister token, a registration can’t be removed. The registry holds the token weakly, so passing the target itself as its own token is common and doesn’t keep the target alive.
Examples
An image viewer caches decoded images by file name in a Map of WeakRef objects. Once an image is collected, its WeakRef is empty, but the map entry stays behind. A registry removes that entry, keyed by the file name it receives as the held value:
> const images = new Map();
undefined
> const cleanup = new FinalizationRegistry((fileName) => {
… if (images.get(fileName)?.deref() === undefined) {
… images.delete(fileName);
… }
… });
undefined
> function cacheImage(fileName, image) {
… images.set(fileName, new WeakRef(image));
… cleanup.register(image, fileName);
… }
undefined
> cacheImage("logo.png", { width: 512, height: 512 })
undefined
> images.size
1
The callback checks .deref() before deleting, since a newer image may have been cached under the same name by the time it runs.
A database client closes its connections explicitly, and a registry acts as a backstop for connections that the program forgot to close. Closing a connection unregisters it, using the connection object as its own token, so the backstop doesn’t close it a second time:
> const backstop = new FinalizationRegistry((id) => {
… console.log(`Connection ${id} leaked, closing it`);
… });
undefined
> function openConnection(id) {
… const connection = { id, open: true };
… backstop.register(connection, id, connection);
… return connection;
… }
undefined
> function closeConnection(connection) {
… connection.open = false;
… return backstop.unregister(connection);
… }
undefined
> const connection = openConnection(7);
undefined
> closeConnection(connection)
true
> closeConnection(connection)
false
The second call returns false because the first one already removed the registration.
Common Gotchas
The held value can’t be the target itself, and only objects and non-registered symbols can be targets:
> const registry = new FinalizationRegistry(() => {});
undefined
> const session = { user: "Ada" };
undefined
> registry.register(session, session)
Uncaught:
TypeError: FinalizationRegistry.prototype.register: target and holdings must not be same
at FinalizationRegistry.register (<anonymous>)
> registry.register("session-42", "Ada")
Uncaught TypeError: FinalizationRegistry.prototype.register: invalid target
at FinalizationRegistry.register (<anonymous>)
A held value that refers to the target in any other way, such as through a property or a closure, doesn’t throw. It silently keeps the target alive, so its callback never runs. An ID or a file name avoids the problem, since nothing in it leads back to the target.
Cleanup that must happen, such as closing a file or releasing a lock, needs an explicit call. The using declaration runs that cleanup at a known point, when its block exits.
Related Resources
Course
JavaScript Map and Set for Pythonistas
Port a community garden's season report to JavaScript, and learn when to use a Map, a Set, or a plain object, and how to count, group, and compare without Counter or defaultdict.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026