There's a lot of open issues
A long-lived project that still receives updates
Explicit soft deletion for ActiveRecord via deleted_at and default scope.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.0.0, < 8.1
 Project Readme

Explicit soft deletion for ActiveRecord via deleted_at + callbacks and optional default scope.
Not overwriting destroy or delete.

Install

gem install soft_deletion

Usage

require 'soft_deletion'

class User < ActiveRecord::Base
  has_soft_deletion default_scope: true

  before_soft_delete :validate_deletability # soft_delete stops if this returns false
  after_soft_delete :send_deletion_emails

  has_many :products
end

# soft delete them including all soft-deletable dependencies that are marked as :destroy, :delete_all, :nullify
user = User.first
user.products.count == 10
user.soft_delete!(validate: false)
user.deleted? # true

# use special with_deleted scope to find them ...
user.reload # ActiveRecord::RecordNotFound
User.with_deleted do
  user.reload # there it is ...
  user.products.count == 0
end

# Do NOT use on assocations: Account.first.users.with_deleted {

# soft undelete them all
user.soft_undelete!
user.products.count == 10

# soft delete many
User.soft_delete_all!(1,2,3,4)

# get soft deleted records
User.soft_deleted

To add the deleted_at to your model, you can either generate a migration using:

rails generate migration add_deleted_at_to_users deleted_at:datetime:index

or create a migration file yourself like:

class AddDeletedAtToUsers < ActiveRecord::Migration[6.0]
  def change
    add_column :users, :deleted_at, :datetime
    add_index :users, :deleted_at
  end
end

TODO

  • has_many :through should delete join associations on soft_delete
  • cascading soft_deletes should use the same timestamp for easy reverts

Authors

Zendesk
michael@grosser.it
License: MIT
CI