MCPcopy
hub / github.com/directus/directus / deleteOne

Method deleteOne

api/src/services/collections.ts:634–813  ·  view source on GitHub ↗

* Delete a single collection This will delete the table and all records within. It'll also * delete any fields, presets, activity, revisions, and permissions relating to this collection

(collectionKey: string, opts?: MutationOptions)

Source from the content-addressed store, hash-verified

632 * delete any fields, presets, activity, revisions, and permissions relating to this collection
633 */
634 async deleteOne(collectionKey: string, opts?: MutationOptions): Promise<string> {
635 if (this.accountability && this.accountability.admin !== true) {
636 throw new ForbiddenError();
637 }
638
639 const nestedActionEvents: ActionEventParams[] = [];
640
641 try {
642 const collections = await this.readByQuery();
643
644 const collectionToBeDeleted = collections.find((collection) => collection.collection === collectionKey);
645
646 if (!!collectionToBeDeleted === false) {
647 throw new ForbiddenError();
648 }
649
650 await transaction(this.knex, async (trx) => {
651 if (collectionToBeDeleted!.schema) {
652 await trx.schema.dropTable(collectionKey);
653 }
654
655 // Make sure this collection isn't used as a group in any other collections
656 await trx('directus_collections').update({ group: null }).where({ group: collectionKey });
657
658 if (collectionToBeDeleted!.meta) {
659 const collectionsItemsService = new ItemsService('directus_collections', {
660 knex: trx,
661 accountability: this.accountability,
662 schema: this.schema,
663 });
664
665 await collectionsItemsService.deleteOne(collectionKey, {
666 bypassEmitAction: (params) =>
667 opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
668 });
669 }
670
671 if (collectionToBeDeleted!.schema) {
672 const fieldsService = new FieldsService({
673 knex: trx,
674 accountability: this.accountability,
675 schema: this.schema,
676 });
677
678 const fieldItemsService = new ItemsService('directus_fields', {
679 knex: trx,
680 accountability: this.accountability,
681 schema: this.schema,
682 });
683
684 await fieldItemsService.deleteByQuery(
685 {
686 filter: {
687 collection: { _eq: collectionKey },
688 },
689 },
690 {
691 bypassEmitAction: (params) =>

Callers 2

deleteManyMethod · 0.95
resolveSystemAdminFunction · 0.95

Calls 15

readByQueryMethod · 0.95
deleteOneMethod · 0.95
deleteByQueryMethod · 0.95
deleteFieldMethod · 0.95
transactionFunction · 0.85
getCollectionMetaUpdatesFunction · 0.85
shouldClearCacheFunction · 0.85
clearSystemCacheFunction · 0.85
getEntitlementManagerFunction · 0.85
getSchemaFunction · 0.85

Tested by

no test coverage detected