Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise - Automatically delete account that hasn't been confirmed after a defined period of time

Everything is in the title, I want to be able to delete account that hasn't been confirmed after a set period of time.

I'm using:

  • Rails 3.0.5
  • Devise 1.3.4

My Devise User model has these attributes:

devise :database_authenticatable, :confirmable, :registerable, :recoverable, :rememberable, :trackable, :validatable

And my db/schema.rb

create_table "users", :force => true do |t|
    t.string    "email",                                 :default => "", :null => false
    t.string    "encrypted_password",     :limit => 128, :default => "", :null => false
    t.string    "reset_password_token"
    t.timestamp "reset_password_sent_at"
    t.timestamp "remember_created_at"
    t.integer   "sign_in_count",                         :default => 0
    t.timestamp "current_sign_in_at"
    t.timestamp "last_sign_in_at"
    t.string    "current_sign_in_ip"
    t.string    "last_sign_in_ip"
    t.timestamp "created_at"
    t.timestamp "updated_at"
    t.string    "confirmation_token"
    t.timestamp "confirmed_at"
    t.timestamp "confirmation_sent_at"
  end

Little bonus - in my User model, I've specify the relationships between my data, such as:

has_one  :user_content,     :dependent => :destroy

I want to make sure that its children are also deleted.

Thanks a lot for anyone who can help !


Thanks to jrdi, here's the answer for a database with SQLite 3

desc "Delete all unconfirmed users after 7 days"
task :delete_unconfirmed_users => :environment do
  users = User.all(:conditions => 'confirmed_at is NULL AND confirmation_sent_at <= datetime(\'now\',\'-7 days\')')
  users.each do |user|
    user.destroy
  end
end

For PostGRE (Heroku)

desc "Delete all unconfirmed users after 7 days"
task :postgre_delete_unconfirmed_users => :environment do
  users = User.all(:conditions => 'confirmed_at is NULL AND confirmation_sent_at <= current_date - integer \'7\' ')
  users.each do |user|
    user.destroy
  end
end

See his answer for full details. Thanks@lot to him.

like image 728
Lucas Avatar asked Dec 28 '22 19:12

Lucas


1 Answers

You can make a rake task that runs with a daemon, cron for example.

In this rake you can get all not confirmed users (:confirmed_at is nil) and get when send the confirmation email :confirmation_sent_at

When you have a users that conforms the especifications make user.destroyand all should be deleted correctly.

If you need a example of this rake task tell me and I make your first aproximation.

--- Edit ---

Create a delete_unconfirmed_users.rake in lib/tasks

desc "Delete all unconfirmed users after 7 days"
task :delete_unconfirmed_users => :environment do
  users = User.all(:conditions => 'confirmed_at is NULL AND confirmation_sent_at >= DATE_SUB(NOW(), INTERVAL 7 day)')
  users.each do |user|
    user.destroy
  end
end

Run rake delete_unconfirmed_users

like image 77
jrdi Avatar answered Jan 13 '23 11:01

jrdi