Scans collections for relationship and upload fields, including nested groups, tabs and arrays, and registers beforeDelete hooks on referenced collections. When a document is deleted, referencing documents are either cascade-deleted or have the field unlinked. Configured through plugin options for allowlists, exclusions and per-field declarations. Marked experimental; works with PostgreSQL and MongoDB adapters.
A Payload CMS plugin that adds support for collection references.
Installation
npm i @lizardglobal/payload-collection-references2
Stars
0
Forks
0
Open Issues
4 months ago
Last Updated
11
Weekly Downloads
v1.0.2
npm Version
76.6 KB
Unpacked Size
0
Dependencies
20 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 2 |
| Dec 4, 2026 | 2 |
| Dec 3, 2026 | 2 |
| Dec 2, 2026 | 2 |
| Dec 1, 2026 | 2 |
| Nov 30, 2026 | 2 |
| Nov 29, 2026 | 2 |
| Nov 28, 2026 | 2 |
| Nov 27, 2026 | 2 |
| Nov 26, 2026 | 2 |
| Nov 25, 2026 | 2 |
| Nov 24, 2026 | 2 |
| Nov 23, 2026 | 2 |
| Nov 22, 2026 | 2 |
| Nov 21, 2026 | 2 |
| Nov 20, 2026 | 2 |
| Nov 19, 2026 | 2 |
| Nov 18, 2026 | 2 |
| Nov 17, 2026 | 2 |
| Nov 16, 2026 | 2 |
| Nov 15, 2026 | 2 |
| Nov 14, 2026 | 2 |
| Nov 13, 2026 | 2 |
| Nov 12, 2026 | 2 |
| Nov 11, 2026 | 2 |
| Nov 10, 2026 | 2 |
| Nov 9, 2026 | 2 |
| Nov 8, 2026 | 2 |
| Nov 7, 2026 | 2 |
| Nov 6, 2026 | 2 |
| Nov 5, 2026 | 2 |
| Nov 4, 2026 | 2 |
| Nov 3, 2026 | 2 |
| Nov 2, 2026 | 2 |
| Nov 1, 2026 | 2 |
| Oct 31, 2026 | 2 |
| Oct 30, 2026 | 2 |
| Oct 29, 2026 | 2 |
| Oct 28, 2026 | 2 |
| Oct 27, 2026 | 2 |
| Oct 26, 2026 | 2 |
| Oct 25, 2026 | 2 |
| Oct 24, 2026 | 2 |
| Oct 23, 2026 | 2 |
| Oct 22, 2026 | 2 |
| Oct 21, 2026 | 2 |
| Oct 20, 2026 | 2 |
| Oct 19, 2026 | 2 |
| Oct 18, 2026 | 2 |
| Oct 17, 2026 | 2 |
| Oct 16, 2026 | 2 |
| Oct 15, 2026 | 2 |
| Oct 14, 2026 | 2 |
| Oct 13, 2026 | 2 |
| Oct 12, 2026 | 2 |
| Oct 11, 2026 | 2 |
| Oct 10, 2026 | 2 |
| Oct 9, 2026 | 2 |
| Oct 8, 2026 | 2 |
| Oct 7, 2026 | 2 |
| Oct 6, 2026 | 2 |
| Oct 5, 2026 | 2 |
| Oct 4, 2026 | 2 |
| Oct 3, 2026 | 2 |
| Oct 2, 2026 | 2 |
| Oct 1, 2026 | 2 |
| Sep 30, 2026 | 2 |
| Sep 29, 2026 | 2 |
| Sep 28, 2026 | 2 |
| Sep 27, 2026 | 2 |
| Sep 26, 2026 | 2 |
| Sep 25, 2026 | 2 |
| Sep 24, 2026 | 2 |
| Sep 23, 2026 | 2 |
| Sep 22, 2026 | 2 |
| Sep 21, 2026 | 2 |
| Sep 20, 2026 | 2 |
| Sep 19, 2026 | 2 |
| Sep 18, 2026 | 2 |
| Sep 17, 2026 | 2 |
| Sep 16, 2026 | 2 |
| Sep 15, 2026 | 2 |
| Sep 14, 2026 | 2 |
| Sep 13, 2026 | 2 |
| Sep 12, 2026 | 2 |
| Sep 11, 2026 | 2 |
| Sep 10, 2026 | 2 |
| Sep 9, 2026 | 2 |
| Sep 8, 2026 | 2 |
| Sep 7, 2026 | 2 |
| Sep 6, 2026 | 2 |
7−2 since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 7 |
| Dec 4, 2026 | 8 |
| Dec 3, 2026 | 9 |
| Dec 2, 2026 | 5 |
| Dec 1, 2026 | 7 |
| Nov 30, 2026 | 8 |
| Nov 29, 2026 | 7 |
| Nov 28, 2026 | 6 |
| Nov 27, 2026 | 5 |
| Nov 26, 2026 | 6 |
| Nov 25, 2026 | 6 |
| Nov 24, 2026 | 4 |
| Nov 23, 2026 | 3 |
| Nov 22, 2026 | 5 |
| Nov 21, 2026 | 5 |
| Nov 20, 2026 | 5 |
| Nov 19, 2026 | 3 |
| Nov 18, 2026 | 5 |
| Nov 17, 2026 | 4 |
| Nov 16, 2026 | 4 |
| Nov 15, 2026 | 3 |
| Nov 14, 2026 | 4 |
| Nov 13, 2026 | 6 |
| Nov 12, 2026 | 6 |
| Nov 11, 2026 | 4 |
| Nov 10, 2026 | 6 |
| Nov 9, 2026 | 6 |
| Nov 8, 2026 | 7 |
| Nov 7, 2026 | 8 |
| Nov 6, 2026 | 7 |
| Nov 5, 2026 | 8 |
| Nov 4, 2026 | 8 |
| Nov 3, 2026 | 8 |
| Nov 2, 2026 | 9 |
| Nov 1, 2026 | 7 |
| Oct 31, 2026 | 5 |
| Oct 30, 2026 | 4 |
| Oct 29, 2026 | 3 |
| Oct 28, 2026 | 3 |
| Oct 27, 2026 | 1 |
| Oct 26, 2026 | 3 |
| Oct 25, 2026 | 3 |
| Oct 24, 2026 | 3 |
| Oct 23, 2026 | 3 |
| Oct 22, 2026 | 6 |
| Oct 21, 2026 | 6 |
| Oct 20, 2026 | 6 |
| Oct 19, 2026 | 3 |
| Oct 18, 2026 | 5 |
| Oct 17, 2026 | 5 |
| Oct 16, 2026 | 5 |
| Oct 15, 2026 | 2 |
| Oct 14, 2026 | 2 |
| Oct 13, 2026 | 2 |
| Oct 12, 2026 | 2 |
| Oct 11, 2026 | 1 |
| Oct 10, 2026 | 1 |
| Oct 9, 2026 | 1 |
| Oct 8, 2026 | 1 |
| Oct 7, 2026 | 1 |
| Oct 6, 2026 | 4 |
| Oct 5, 2026 | 4 |
| Oct 4, 2026 | 5 |
| Oct 3, 2026 | 7 |
| Oct 2, 2026 | 7 |
| Oct 1, 2026 | 7 |
| Sep 30, 2026 | 9 |
| Sep 29, 2026 | 6 |
| Sep 28, 2026 | 7 |
| Sep 27, 2026 | 5 |
| Sep 26, 2026 | 7 |
| Sep 25, 2026 | 7 |
| Sep 24, 2026 | 7 |
| Sep 23, 2026 | 7 |
| Sep 22, 2026 | 8 |
| Sep 21, 2026 | 9 |
| Sep 20, 2026 | 9 |
| Sep 19, 2026 | 5 |
| Sep 18, 2026 | 5 |
| Sep 17, 2026 | 6 |
| Sep 16, 2026 | 4 |
| Sep 15, 2026 | 6 |
| Sep 14, 2026 | 4 |
| Sep 13, 2026 | 4 |
| Sep 12, 2026 | 5 |
| Sep 11, 2026 | 5 |
| Sep 10, 2026 | 6 |
| Sep 9, 2026 | 7 |
| Sep 8, 2026 | 5 |
| Sep 7, 2026 | 7 |
| Sep 6, 2026 | 9 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1272063083-root)<a href="https://payloaddirectory.dev/plugins/1272063083-root"><img src="https://payloaddirectory.dev/badge/1272063083-root.svg" alt="Payload Directory"></a>README
@lizardglobal/payload-collection-referencesPayload CMS plugin for automatically managing collection references on document deletion.
[!WARNING] This plugin is still experimental. APIs, collection schemas, and behavior may change without a stable compatibility guarantee. Use in production with caution and pin versions deliberately.
relationship and upload fields in your collectionsbeforeDelete hooks to cascade-delete or unlink references^3.0.0>=20pnpm add payload-collection-references
Add the plugin to your payload.config.ts:
import { collectionReferencesPlugin } from 'payload-collection-references'
export default buildConfig({
collections: [
{
slug: 'posts',
fields: [
{
name: 'author',
type: 'relationship',
relationTo: 'users',
},
{
name: 'heroImage',
type: 'upload',
relationTo: 'media',
},
],
},
],
plugins: [
collectionReferencesPlugin(),
],
})
That's it. The plugin auto-discovers all relationship and upload fields and registers them with a default onDelete: 'delete' strategy.
At config initialization time, the plugin:
relationship and upload fields with their dot-notation pathsbeforeDelete hook on each collection that other collections referenceWhen a document is deleted, the hook finds all documents in referencing collections and either cascade-deletes them or unlinks the field — depending on your onDelete strategy.
collectionReferencesPlugin(options?: CollectionReferencesPluginOptions)
| Option | Type | Description |
|---|---|---|
declarations | Partial<Record<CollectionSlug, CollectionReferenceDeclaration[]>> | Explicit per-collection reference declarations. Keys are the collection slugs that own the declared fields. |
collections | CollectionSlug[] | Allowlist of collection slugs to apply the plugin to. Cannot be combined with exclude. |
exclude | CollectionSlug[] | Blocklist of collection slugs to skip. Cannot be combined with collections. |
disabled | boolean | When true, the plugin is a no-op. Useful for environment-based toggling. |
type CollectionReferenceDeclaration = {
fieldPath: string // Dot-notation path to the relationship field
onDelete: 'delete' | 'unlink' // What to do when the referenced document is deleted
referencedCollection: CollectionSlug // The collection the field points to (the relationship target)
unlinkValue?: [] | null // Value to set when unlinking (defaults to null)
}
collectionReferencesPlugin()
collectionReferencesPlugin({
collections: ['posts', 'authors'],
})
collectionReferencesPlugin({
exclude: ['media'],
})
Auto-discovery defaults to onDelete: 'delete'. Use explicit declarations to override this per field:
collectionReferencesPlugin({
declarations: {
// "posts" owns both of these relationship fields:
posts: [
{
fieldPath: 'heroImage', // field in "posts" that references "media"
onDelete: 'unlink', // when the media doc is deleted, set heroImage to null
referencedCollection: 'media',
},
{
fieldPath: 'author', // field in "posts" that references "users"
onDelete: 'delete', // when the user is deleted, cascade-delete their posts
referencedCollection: 'users',
},
],
},
})
collectionReferencesPlugin({
disabled: process.env.NODE_ENV === 'test',
})
The plugin supports dot-notation for fields nested inside groups or tabs:
collectionReferencesPlugin({
declarations: {
posts: [
{
fieldPath: 'seo.ogImage', // nested inside a "seo" group, owned by "posts"
onDelete: 'unlink',
referencedCollection: 'media',
},
],
},
})
onDelete Strategies| Strategy | Behavior |
|---|---|
'delete' | Cascade-deletes all documents in the referencing collection that point to the deleted document. Triggers their own beforeDelete hooks recursively. |
'unlink' | Sets the relationship field to null (or a custom unlinkValue) on all matching documents. The referencing documents are preserved. |
The plugin tracks processed references per request via req.context to prevent infinite loops when collections reference each other circularly.
If you need finer control, you can wire up collections manually:
import {
withCollectionReferences,
finalizeCollectionReferences,
} from 'payload-collection-references'
const postsCollection = withCollectionReferences(
{
slug: 'posts',
fields: [
{ name: 'author', type: 'relationship', relationTo: 'users' },
],
},
[
// Explicit declaration overriding the auto-discovered default
{ fieldPath: 'author', onDelete: 'unlink', referencedCollection: 'users' },
],
)
const collections = finalizeCollectionReferences([postsCollection, usersCollection])
import {
buildWhereForReferences, // Build a Payload Where clause for a set of references
filterUploadCollections, // Filter a collection list to only upload collections
getCollectionConfig, // Get a CollectionConfig by slug from a request
getReferencesPointingTo, // Get all registered references pointing to a collection
getRegisteredCollectionReferences, // Get the full reference registry
groupReferencesByCollection, // Group references by their owning collection
} from 'payload-collection-references'
getCollectionConfig(req, collectionSlug)Returns the CollectionConfig matching a slug, read from the Payload config resolved on the request.
getCollectionConfig(req: PayloadRequest, collectionSlug: CollectionSlug): CollectionConfig | undefined
| Param | Type | Description |
|---|---|---|
req | PayloadRequest | Payload request, used to access req.payload.config.collections |
collectionSlug | CollectionSlug | Slug of the collection to look up |
Returns: the matching CollectionConfig, or undefined if it doesn't exist.
const postsConfig = getCollectionConfig(req, 'posts')
groupReferencesByCollection(references)Groups a list of registered references by their owning collection (reference.collection).
groupReferencesByCollection(
references: RegisteredCollectionReference[]
): Map<CollectionSlug, RegisteredCollectionReference[]>
| Param | Type | Description |
|---|---|---|
references | RegisteredCollectionReference[] | References to group |
Returns: a Map where each key is a collection slug and each value is the list of references owned by that collection.
const grouped = groupReferencesByCollection(getRegisteredCollectionReferences())
const postsRefs = grouped.get('posts')
buildWhereForReferences(references, documentId)Builds a Payload Where clause matching any document whose reference field points to documentId. Combines multiple references with or when needed.
buildWhereForReferences(
references: RegisteredCollectionReference[],
documentId: string
): Where
| Param | Type | Description |
|---|---|---|
references | RegisteredCollectionReference[] | References whose fieldPath will be tested |
documentId | string | ID of the target document (tested with equals) |
Returns: a Where object — the single reference directly if references.length === 1, otherwise { or: [...] }.
const where = buildWhereForReferences(refs, deletedDoc.id)
const affected = await req.payload.find({ collection: 'posts', where })
getReferencesPointingTo(collectionSlug)Filters the global reference registry down to entries whose referencedCollection matches the given slug.
getReferencesPointingTo(collectionSlug: CollectionSlug): RegisteredCollectionReference[]
| Param | Type | Description |
|---|---|---|
collectionSlug | CollectionSlug | The referenced collection to look up |
Returns: all references (auto-discovered or declared) that point to this collection — this is what the beforeDelete hook queries to know what to cascade/unlink.
const refsToMedia = getReferencesPointingTo('media')
getRegisteredCollectionReferences()Returns the full registry of registered references, across all collections (auto-discovered + explicit declarations, merged at init time).
getRegisteredCollectionReferences(): RegisteredCollectionReference[]
Returns: the raw list of every RegisteredCollectionReference known to the plugin. Used as the source for groupReferencesByCollection and getReferencesPointingTo.
const all = getRegisteredCollectionReferences()
filterUploadCollections(collections)Filters a list of CollectionConfig down to upload collections only (upload: true or an upload config object). Doesn't require a req — usable at config build time.
filterUploadCollections(collections: CollectionConfig[]): CollectionConfig[]
| Param | Type | Description |
|---|---|---|
collections | CollectionConfig[] | List of collections to filter |
Returns: the subset of collections with a truthy upload.
const uploadCollections = filterUploadCollections(payloadConfig.collections)
Note:
getUploadCollectionConfigs(req)does the same thing but readsreq.payload.config.collectionsdirectly — no need to pass in the list.
Set DEBUG=true in your environment to enable verbose logging:
DEBUG=true pnpm dev
This logs every auto-discovered relationship, every cleanup hook invocation, and every cascade or unlink operation performed.
The plugin is fully typed. CollectionSlug comes from Payload and resolves against your generated payload-types.ts, so collection slugs in declarations are type-safe and autocompleted.
import type {
CollectionReferenceDeclaration,
CollectionReferencesPluginOptions,
CollectionSlug,
RegisteredCollectionReference,
} from 'payload-collection-references'
payload relationship enhancer
Referential integrity and bi-directional syncing for Payload relationship fields
31901/wk12v2
payload db check
Admin view that finds duplicates, broken references and one-sided links, with opt-in previewed repairs
399/wk0v3
payloadcms relationships
List incoming and outgoing relationships for any Payload document via a synced relationships collection
101v2
payload dependency graph
Tracks dependencies between collections and globals so you know which content depends on which
032/wk14v2
payload locale snapshot
Prevents loss of other-locale translations in localized blocks and arrays when saving with the Postgres adapter
6663/wk3v3
payload seed
Type-safe database seeding for Payload with typed cross-file references and inline media uploads
57108/wk18v3