Lucky Logo

# Delete Operations

Similar to the SaveOperation, Avram comes with a DeleteOperation that’s generated with each model. This allows you to write more complex logic around deleteing records. (e.g. delete confirmations, etc…)

# Simple deletes

If you just want to delete a record without any validations or callbacks, the simplest way is to use the generated {ModelName}::DeleteOperation

For example, if we have a Server model, Lucky will generate a Server::DeleteOperation that you can use:

server = ServerQuery.find(123)

If the record fails to be deleted, an Avram::InvalidOperationError will be raised.

# Setting up a custom DeleteOperation

You can customize DeleteOperations with callbacks and validations. These classes go in your src/operations/ directory, and will inherit from {ModelName}::DeleteOperation.

# src/operations/
class DeleteServer < Server::DeleteOperation

# Using a DeleteOperation in actions

The interface should feel pretty familiar. The object being deleted is passed in to the delete method, and a block will return the operation instance, and the object being deleted.

# src/actions/servers/
class Servers::Delete < BrowserAction
  delete "/servers/:server_id" do
    server = ServerQuery.find(server_id)

    DeleteServer.delete(server) do |operation, deleted_server|
      if operation.deleted?
        redirect to: Servers::Index
        flash.failure = "Could not delete"
        html Servers::EditPage, server: deleted_server

You can also pass in params or named args for use with attributes, or needs.

DeleteServer.delete(server, params, secret_codes: [23_u16, 94_u16]) do |operation, deleted_server|
  if operation.deleted?
    redirect to: Servers::Index
    flash.failure = "Could not delete"
    html Servers::EditPage, server: deleted_server

# Delete and raise if it fails

You can also use the delete! method if you don’t need validations and expect deletes to work every time:


This is helpful when your operation only has callbacks or needs and is expected to work every time.

# DeleteOperation Callbacks and Validations

DeleteOperations come with before_delete and after_delete callbacks that allow you to either validate some code before performing the delete, or perform some action after deleteing. (i.e. Send a “Goodbye” email, etc…)

Along with the callbacks, you also have access to attribute, needs, and all of the columns related to a model. You even have file_attribute for those times you need to use biometric scans to authorize deleting a record!

# before_delete

# src/operations/
class DeleteServer < Server::DeleteOperation
  attribute confirmation : String

  before_delete do
    validate_required confirmation

    # `record` is the object to be deleted
    if confirmation.value != record.server_name
      confirmation.add_error("Confirmation must match the server name")

# after_delete

# src/operations/
class DeleteServer < Server::DeleteOperation
  needs secret_codes : Array(UInt16)

  after_delete do |deleted_server|
    decrypted_server_data =, with: secret_codes)

# Bulk delete

Currently bulk deletes with DeleteOperation are not supported.

If you need to bulk delete a group of records based on a where query, you can use delete at the end of your query. This returns the number of records deleted.


# Soft Deletes

A “soft delete” is when you want to hide a record as if it were deleted, but you want to keep the actual record in your database. This allows you to restore the record without losing any previous data or associations.

Avram comes with some built-in modules to help make working with soft deleted records a lot easier. Let’s add it to an existing Article model.

  • First, we need to add a new soft_deleted_at : Time? column to the table that needs soft deletes.
# Run this in your terminal
lucky gen.migration AddSoftDeleteToArticles
  • Open your new db/migrations/ file.
def migrate
  alter table_for(Article) do
    add soft_deleted_at : Time?
  • Now open your src/models/ file.
class Article < BaseModel
  # Include this module to add methods for
  # soft deleting and restoring
  include Avram::SoftDelete::Model

  table do
    # Add the new column to your model
    column soft_deleted_at : Time?
  • Next you need to update src/queries/
class ArticleQuery < Article::BaseQuery
  # Include this module to add methods for
  # querying and soft deleting records
  include Avram::SoftDelete::Query

# Marking a record as soft deleted

Once a model includes the Avram::SoftDelete::Model, the associated DeleteOperation will handle the soft delete for you.

# src/operations/
class DeleteArticle < Article::DeleteOperation

and in your action

# src/actions/articles/
class Articles::Delete < BrowserAction
  delete "/articles/:article_id" do
    article = ArticleQuery.find(article_id)

    deleted_article = DeleteArticle.delete!(article)

    # This returns `true`

    redirect to: Articles::Index

# Soft deleting in bulk

Currently bulk soft deletes with DeleteOperation are not supported.

You can bulk update a group of records as soft deleted with the soft_delete method on your Query object.

articles_to_delete =

# Marks the articles created over 3 years ago as soft deleted

# Restore a soft deleted record

If you need to restore a soft deleted record, you can use the restore method on the model instance.

# Set the `soft_deleted_at` back to `nil`

# Bulk restoring soft deleted records

The same as we can bulk soft delete records, we can also bulk update to restore them with the restore method on your Query object.

articles_to_restore =

# Restore recently published articles

# Query soft deleted records

# Return all articles that are not soft deleted

# Return all articles that are soft deleted

# Default queries without soft deleted

If you want to filter out soft deleted records by default, it’s really easy to do. Just add the only_kept method as the default query in the initialize method.

class ArticleQuery < Article::BaseQuery
  include Avram::SoftDelete::Query

  # All queries will scope to only_kept
  def initialize
    defaults &.only_kept
# Return all articles that are not soft deleted

Even with your default scope, you can still return soft deleted records when you need.

# Return all articles, both `kept` and soft deleted

# Truncating

# Truncate table

If you need to delete every record in the entire table, you can use truncate.



Running the truncate method may raise an error similar to the following:

Error message cannot truncate a table referenced in a foreign key constraint.

If that’s the case, call the same method with the cascade option set to true:

UserQuery.truncate(cascade: true)

This will automatically delete or update matching records in a child table where a foreign key relationship is in place.

# Truncate database

You can also truncate your entire database by calling truncate on your database class.


This method is great for tests; horrible for production. Also note this method is not chainable.

See a problem? Have an idea for improvement? Edit this page on GitHub