Elegant soft delete solution for Ruby on Rails applications
π Documentation β’ π Changelog β’ π¬ Discussions β’ π RubyGems
A simple and elegant soft delete solution for Rails applications with a beautiful web interface to manage your deleted records.
RecycleBin provides a "trash can" or "recycle bin" functionality where deleted records are marked as deleted instead of being permanently removed from your database. You can easily restore them or permanently delete them through a clean web interface.
- Soft Delete: Records are marked as deleted instead of being permanently removed
- Web Interface: Beautiful, responsive dashboard to view and manage deleted items
- Easy Integration: Simple module inclusion in your models
- Bulk Operations: Restore or permanently delete multiple items at once
- Model Filtering: Filter deleted items by model type and time
- Rails Generators: Automated setup with generators
- Configurable: Flexible configuration options
- Statistics Dashboard: Overview of your deleted items
Add this line to your application's Gemfile:
gem 'recycle_bin', '~> 1.1'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install recycle_bin
$ rails generate recycle_bin:install
This will:
- Create a configuration file at
config/initializers/recycle_bin.rb
- Add the mount point to your routes
- Display setup instructions
For each model you want to soft delete, run:
$ rails generate recycle_bin:add_deleted_at ModelName
For example:
$ rails generate recycle_bin:add_deleted_at User
$ rails generate recycle_bin:add_deleted_at Post
$ rails generate recycle_bin:add_deleted_at Comment
This generates a migration to add the deleted_at
column and index.
$ rails db:migrate
Add RecycleBin::SoftDeletable
to any model you want to soft delete:
class User < ApplicationRecord
include RecycleBin::SoftDeletable
end
class Post < ApplicationRecord
include RecycleBin::SoftDeletable
end
class Comment < ApplicationRecord
include RecycleBin::SoftDeletable
end
Navigate to /recycle_bin
in your Rails application to see the web interface!
Once you've included the RecycleBin::SoftDeletable
module in your models:
# Create a record
user = User.create(name: "John Doe", email: "john@example.com")
# Soft delete (goes to trash)
user.destroy
# or
user.soft_delete
# Check if deleted
user.deleted? # => true
# Restore from trash
user.restore
# Permanently delete (careful!)
user.destroy!
The module provides several useful scopes:
# Get all active (non-deleted) records (default scope)
User.all
# Get only deleted records
User.deleted
# Get all records including deleted ones
User.with_deleted
# Get only deleted records (alias)
User.only_deleted
# Get non-deleted records explicitly
User.not_deleted
# Restore a deleted record by ID
User.restore(123)
The web interface (/recycle_bin
) provides:
- Dashboard Overview: Statistics about deleted items
- Item Listing: View all deleted records with details
- Filtering: Filter by model type (User, Post, Comment, etc.)
- Time Filters: View items deleted today, this week, or this month
- Individual Actions: Restore or permanently delete single items
- Bulk Actions: Select multiple items to restore or delete
- Item Details: Click on any item to see full details and history
Configure RecycleBin in config/initializers/recycle_bin.rb
:
RecycleBin.configure do |config|
# Enable/disable web interface (default: true)
config.enable_web_interface = true
# Items per page in web interface (default: 25)
config.items_per_page = 50
# Auto-cleanup items after specified time (default: nil - disabled)
config.auto_cleanup_after = 30.days
# Method to get current user for audit trail (default: :current_user)
config.current_user_method = :current_user
# Authorization callback - restrict access to admins only
config.authorize_with do |controller|
# Example: Only allow admins
controller.current_user&.admin?
end
end
To restrict access to the web interface, use the authorize_with
configuration:
RecycleBin.configure do |config|
config.authorize_with do |controller|
# Only allow admins
controller.current_user&.admin?
end
end
If authorization fails, users will be redirected with an "Access denied" message.
By default, RecycleBin tries to use title
, name
, or email
fields for display. You can customize this:
class User < ApplicationRecord
include RecycleBin::SoftDeletable
def recyclable_title
"#{first_name} #{last_name} (#{email})"
end
end
RecycleBin works with Rails associations. The web interface will show related items:
class User < ApplicationRecord
include RecycleBin::SoftDeletable
has_many :posts, dependent: :destroy
end
class Post < ApplicationRecord
include RecycleBin::SoftDeletable
belongs_to :user
has_many :comments, dependent: :destroy
end
Get statistics about your deleted items:
# Get overall statistics
RecycleBin.stats
# => { deleted_items: 45, models_with_soft_delete: ["User", "Post", "Comment"] }
# Count deleted items across all models
RecycleBin.count_deleted_items # => 45
# Get models that have soft delete enabled
RecycleBin.models_with_soft_delete # => ["User", "Post", "Comment"]
soft_delete
- Mark record as deletedrestore
- Restore deleted recorddeleted?
- Check if record is deleteddestroy
- Soft delete (overrides Rails default)destroy!
- Permanently delete from databaserecyclable_title
- Display title for web interface
.deleted
- Scope for deleted records only.not_deleted
- Scope for active records only.with_deleted
- Scope for all records including deleted.only_deleted
- Alias for.deleted
.restore(id)
- Restore a record by ID.deleted_records
- Get all deleted records
enable_web_interface
- Enable/disable web UI (default: true)items_per_page
- Pagination limit (default: 25)auto_cleanup_after
- Auto-delete after time period (default: nil)current_user_method
- Method to get current user (default: :current_user)authorize_with
- Authorization callback block
$ rails generate recycle_bin:install
Sets up RecycleBin in your Rails application.
$ rails generate recycle_bin:add_deleted_at ModelName
Adds the deleted_at
column to the specified model.
The gem adds these routes to your application:
GET /recycle_bin # Dashboard/index
GET /recycle_bin/trash # List all deleted items
GET /recycle_bin/trash/:model_type/:id # Show specific item
PATCH /recycle_bin/trash/:model_type/:id/restore # Restore item
DELETE /recycle_bin/trash/:model_type/:id # Permanently delete
PATCH /recycle_bin/trash/bulk_restore # Bulk restore
DELETE /recycle_bin/trash/bulk_destroy # Bulk delete
- Ruby: >= 2.7.0
- Rails: >= 6.0
- Database: Any database supported by Rails (PostgreSQL, MySQL, SQLite, etc.)
class Product < ApplicationRecord
include RecycleBin::SoftDeletable
def recyclable_title
"#{name} - #{sku}"
end
end
# Soft delete a product
product = Product.find(1)
product.destroy # Goes to trash, can be restored
# View deleted products in admin panel at /recycle_bin
class Post < ApplicationRecord
include RecycleBin::SoftDeletable
belongs_to :author, class_name: 'User'
def recyclable_title
title.truncate(50)
end
end
class Comment < ApplicationRecord
include RecycleBin::SoftDeletable
belongs_to :post
belongs_to :user
def recyclable_title
"Comment by #{user.name}: #{body.truncate(30)}"
end
end
class User < ApplicationRecord
include RecycleBin::SoftDeletable
def recyclable_title
"#{name} (#{email})"
end
end
# Admin can restore accidentally deleted users
User.deleted.each do |user|
puts "Deleted user: #{user.recyclable_title}"
end
1. "uninitialized constant RecycleBin" error
- Make sure you've added the gem to your Gemfile and run
bundle install
- Restart your Rails server
2. Routes not working
- Ensure you've run
rails generate recycle_bin:install
- Check that
mount RecycleBin::Engine => '/recycle_bin'
is in yourconfig/routes.rb
3. Records not appearing in trash
- Verify you've included
RecycleBin::SoftDeletable
in your model - Ensure the
deleted_at
column exists (run the migration) - Check that you're calling
.destroy
not.delete
4. Web interface shows "Access denied"
- Check your authorization configuration in the initializer
- Ensure the current user meets your authorization requirements
Enable debug logging to see what's happening:
# In development.rb or console
Rails.logger.level = :debug
Bug reports and pull requests are welcome on GitHub at https://github.com/R95-del/recycle_bin.
The gem is available as open source under the terms of the MIT License.
- Fixed critical bug in user authentication flow
- Resolved memory leak in background job processing
- Corrected deprecation warnings for Rails 7.1 compatibility
- Fixed race condition in concurrent database writes
- Improved error handling in API responses
- Updated dependency versions for security patches
- Enhanced logging for better debugging experience
- Patched potential XSS vulnerability in form helpers
- Updated vulnerable dependencies to secure versions
- Proper pagination: Navigate through all deleted records with page controls
- Configurable page sizes: Choose 25, 50, 100, or 250 items per page
- Accurate item counting: Shows real total counts instead of limited counts
- Enhanced statistics: Added today/week deletion counts
- Better performance: Optimized handling of large datasets
- Per-page controls: User-selectable items per page options
- Memory optimization: DeletedItemsCollection class for efficient data handling
- Removed artificial limits: No more 25/100 item display limits that prevented showing all records
- Pagination persistence: Filters maintained across page navigation
- Memory usage: Better handling of large datasets without loading all into memory
- Count accuracy: Total counts now reflect actual database records
- Performance bottlenecks: Eliminated inefficient loading of all records at once
- TrashController: Complete rewrite with proper pagination logic
- Index view: Enhanced UI with comprehensive pagination controls and statistics
- RecycleBin module: Improved counting methods and performance optimizations
- Statistics calculation: More efficient counting without loading full record sets
- Large dataset support: Now efficiently handles 5000+ deleted records
- Lazy loading: Only loads current page items, not all records
- Optimized queries: Better database query patterns for counting and filtering
- Memory efficient: Reduced memory footprint for large trash collections
- Added
DeletedItemsCollection
class for efficient pagination - Implemented proper offset/limit handling
- Enhanced filtering with maintained pagination state
- Improved error handling for large datasets
- Initial release of RecycleBin gem
- Soft delete functionality for ActiveRecord models
- Web interface for managing trashed items
- Restore functionality for deleted records
- Bulk operations for multiple items
- JSON API support for programmatic access
- Rishi Somani
- Shobhit Jain
- Raghav Agrawal
Made with β€οΈ for the Rails community
Need help? Open an issue on GitHub or check out the web interface at /recycle_bin
in your Rails app.