Soft deletes for ActiveRecord done right
open sourceMITself-hostablefree
Discard adds soft deletes to Active Record models with a discarded_at timestamp: discard and undiscard set and clear it, kept and discarded scopes filter records, and callbacks can run before, after or around each. Unlike paranoia and acts_as_paranoid it overrides no Active Record methods (destroy and delete still delete) and adds no default scope, so discarded records are hidden only where you ask, and associated records are left alone unless your scopes or callbacks handle them. John Hawthorn, who helped maintain paranoia, wrote it in 2017; the README calls it feature-complete, and 2.0 (May 2026) requires Active Record 7.0 or later.
Problems it solves
- hide deleted records in Rails without overriding destroy or adding a default scope
- archive a record so users can't see it but keep the row for history
- let an admin restore something that was removed by mistake
- soft-deleting a parent record shouldn't quietly delete all its child records
- keep a user's comments in their history after the post they were on was removed
- stop deactivated users from signing in while keeping their account row
- move off a paranoia-style gem but keep using the existing deleted_at column
- run code when a record is archived, like archiving its children too
- flag records as inactive with a timestamp and query only the active ones
Features
- discard, discard!, undiscard and undiscard! on records
- discard_all and undiscard_all on scopes
- kept, undiscarded, discarded and with_discarded scopes
- discarded?, undiscarded? and kept? predicates
- before, after and around discard and undiscard callbacks
- discard and undiscard run in a transaction, so a callback error rolls back
- custom column name (keep deleted_at from paranoia)
- no overridden Active Record methods and no default scope
Limitations
- Doesn't discard associated records recursively: there's no dependent: :destroy equivalent, so you use scopes or callbacks1
- Doesn't restore associated records recursively1
- Doesn't adjust counter cache columns: counts include discarded records1
Capabilities
| Has an API | plan not known |
|---|
Alternatives
The same job
These do what Discard does.
- Paranoia acts_as_paranoid for Rails 5, 6 and 7
