44-Nodejs Course 2023: Destroying The Model
Tags: node, typescript, mongodb, fastify
Published: 2022-11-06 18:56:03

---
title: 44-Nodejs Course 2023: Destroying The Model
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejy02wvb085l925aeju5.png
# Use a ratio of 100:42 for best results.
---

We've proceeded with a nice progress in the saving process, now let's go to the deleting step of the model.

## Destroy Method

It actually works exactly like the `delete` method except that the `destroy` method will be called from the model itself directly.

```ts
// src/core/database/model/model.ts
  /**
   * Delete the document from database
   */
  public async destroy() {
    await queryBuilder.deleteOne(this.getCollectionName(), {
      _id: this.data._id,
    });
  }
```

Nothing new here, we just called the query builder to delete the document from the database.

Now the model will keep the data in the memory, but it will be deleted from the database.

Let's give it a try

```ts
import User from './models/users';

const user = await User.find(1);

if (user) {
    await user.destroy();
}
```

And that's it!

But what if we called it in a model that does not have an id?

In that case we shall skip the deletion process and just return the model instance.

```ts
// src/core/database/model/model.ts
  /**
   * Delete the document from database
   */
  public async destroy() {
    if (! this.data._id) return;

    await queryBuilder.deleteOne(this.getCollectionName(), {
        _id: this.data._id,
      });
  }
```

## 🎨 Conclusion

We added a new method called `destroy` which will delete the document from the database using the model itself.

## 🚀 Project Repository

You can find the latest updates of this project on [Github](https://github.com/hassanzohdy/nodejs-2023)

## 😍 Join our community

Join our community on [Discord](https://discord.gg/pb2vmdfhGf) to get help and support (Node Js 2023 Channel).

## 🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on [Youtube](https://www.youtube.com/playlist?list=PLGO8ntvxgiZMJc7RN2lIq9WmMOlWZGzmz), the course is in Arabic language.

## 💰 Bonus Content 💰

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

- [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
- [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
- [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

- [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
- [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
- [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

- [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

- [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)