Skip to content

Delete data from database

Following methods allow you to delete data from the database.

  • delete(each: bool = False, **kwargs) -> int

  • Model

    • Model.delete() method
  • QuerysetProxy

    • QuerysetProxy.remove() method
    • QuerysetProxy.clear() method

delete

delete(each: bool = False, **kwargs) -> int

QuerySet level delete is used to delete multiple records at once.

You either have to filter the QuerySet first or provide a each=True flag to delete whole table.

If you do not provide this flag or a filter a QueryDefinitionError will be raised.

Return number of rows deleted.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import databases
import ormar
import sqlalchemy

database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()


class Book(ormar.Model):
    class Meta:
        tablename = "books"
        metadata = metadata
        database = database

    id: int = ormar.Integer(primary_key=True)
    title: str = ormar.String(max_length=200)
    author: str = ormar.String(max_length=100)
    genre: str = ormar.String(max_length=100, default='Fiction',
                              choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])


await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')

# delete accepts kwargs that will be used in filter
# acting in same way as queryset.filter(**kwargs).delete()
await Book.objects.delete(genre='Fantasy')  # delete all fantasy books
all_books = await Book.objects.all()
assert len(all_books) == 2

Model methods

Each model instance have a set of methods to save, update or load itself.

delete

You can delete model instance by calling delete() method on it.

Tip

Read more about delete() method in models methods

QuerysetProxy methods

When access directly the related ManyToMany field as well as ReverseForeignKey returns the list of related models.

But at the same time it exposes subset of QuerySet API, so you can filter, create, select related etc related models directly from parent model.

remove

Removal of the related model one by one.

Removes the relation in the database.

If you specify the keep_reversed flag to False ormar will also delete the related model from the database.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Album(ormar.Model):
    class Meta:
        tablename = "albums"
        metadata = metadata
        database = database

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    is_best_seller: bool = ormar.Boolean(default=False)

class Track(ormar.Model):
    class Meta:
        tablename = "tracks"
        metadata = metadata
        database = database

    id: int = ormar.Integer(primary_key=True)
    album: Optional[Album] = ormar.ForeignKey(Album)
    title: str = ormar.String(max_length=100)
    position: int = ormar.Integer()
    play_count: int = ormar.Integer(nullable=True)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
album = await Album(name="Malibu").save()
track1 = await Track(
    album=album, title="The Bird", position=1, play_count=30, 
).save()
# remove through proxy from reverse side of relation
await album.tracks.remove(track1, keep_reversed=False)

# the track was also deleted
tracks = await Track.objects.all()
assert len(tracks) == 0

clear

Removal of all related models in one call.

Removes also the relation in the database.

If you specify the keep_reversed flag to False ormar will also delete the related model from the database.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Album(ormar.Model):
    class Meta:
        tablename = "albums"
        metadata = metadata
        database = database

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    is_best_seller: bool = ormar.Boolean(default=False)

class Track(ormar.Model):
    class Meta:
        tablename = "tracks"
        metadata = metadata
        database = database

    id: int = ormar.Integer(primary_key=True)
    album: Optional[Album] = ormar.ForeignKey(Album)
    title: str = ormar.String(max_length=100)
    position: int = ormar.Integer()
    play_count: int = ormar.Integer(nullable=True)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
album = await Album(name="Malibu").save()
track1 = await Track(
    album=album, 
    title="The Bird", 
    position=1, 
    play_count=30, 
).save()
track2 = await Track(
    album=album,
    title="Heart don't stand a chance",
    position=2,
    play_count=20,
).save()

# removes the relation only -> clears foreign keys on tracks
await album.tracks.clear()

# removes also the tracks
await album.tracks.clear(keep_reversed=False)