Skip to main content

removeDefaultScope

The removeDefaultScope method lifts one named default scope from a query. Consider a model where a soft delete pattern has been applied, like so:

@SoftDelete()
export default class Pet extends ApplicationModel {}

When a Pet record is destroyed, the SoftDelete decorator will kick in, preventing the destroy and instead setting the deletedAt field. The decorator also applies a dream:SoftDelete default scope to the model class, preventing records with a non-null deletedAt from showing up, which means you will not be able to find this record again. To get around this, name that scope:

await Pet.count() // 0
await Pet.removeDefaultScope('dream:SoftDelete').count() // 1

Naming the scope lifts that scope and only that scope, on the root query and on associations alike, so every other default scope the model (or an association's model) carries stays in place — including the dream:STI scope restricting a child model to its own type, and any your application declared to control access:

// Removes only the soft-delete scope; dream:STI still restricts results to Bedroom rows
await Bedroom.removeDefaultScope('dream:SoftDelete').all()

// Also works through an association
await user.associationQuery('places').removeDefaultScope('dream:SoftDelete').all()

// Works the same for an identity lookup
await Place.removeDefaultScope('dream:SoftDelete').findOrFail(id)