MateoC
MateoC

Reputation: 29

Automatically delete all records after 30 days

I have a concept for a rails app that I want to make. I want a model that a user can create a record of with a boolean attribute. After 30 days/Month unless the record has true boolean attribute, the record will automatically delete itself.

Upvotes: 1

Views: 2156

Answers (4)

slowjack2k
slowjack2k

Reputation: 2586

I want to mention a non Rails approach. It depends on your database. When you use mongodb you can utilize mongodb "Expire Data from Collections" feature. When you use mysql you can utilize mysql event scheduler. You can find a good example here What is the best way to delete old rows from MySQL on a rolling basis?

Upvotes: 0

JohnDel
JohnDel

Reputation: 2102

There are a couple of options for achieving this:

  1. Whenever and run a script or rake task
  2. Clockwork and run a script or rake task
  3. Background jobs which in my opinion is the "rails way".

For the 1,2 you need to check everyday if a record is 30 days old and then delete it if there isn't a true boolean (which means, checking all the records or optimize the query everyday and check only the 30 days old records etc...). For the 3rd option, you can schedule on the record creation, a job to run after 30 days and do the check for each record independently. It depends on how you are processing the jobs, for example, if you use sidekiq you can use scheduled jobs or if you use resque check resque-scheduler.

Upvotes: 0

Jim Van Fleet
Jim Van Fleet

Reputation: 1118

Performing the deletion is straightforward: create a class method (e.g Record.prune) on the record class in question that performs the deletion based on a query e.g. Record.destroy_all(retain: false) where retain is the boolean attribute you mention. I'd recommend then defining a rake task in lib/tasks that invokes this method (e.g.)

namespace :record
  task :prune => :environment
    Record.prune
  end
end

The scheduling is more difficult; a crontab entry is sufficient to provide the correct timing, but ensuring that an appropriate environment (e.g. one that's loaded rbenv/rvm and any appropriate environment variables) is more difficult. Ensuring that your deployment process produces binstubs is probably helpful here. From there the bin/rake record:prune ought to be enough. It's hard to provide a more in-depth answer without more knowledge of all the environments in which you hope to accomplish this task.

Upvotes: 0

Hans Findel
Hans Findel

Reputation: 136

In rails 5 you have access to "active_job"(http://guides.rubyonrails.org/active_job_basics.html)

There are two simple ways.

After creating the record, you could set this job to be executed after 30 days. This job checks if the record matches the specifications.

The other alternative is to create an alternative job, that runs everyday which queries the database for every record (of this specific model) that where created 30 days ago and destroy them if they do not match the specifications. (If thats on the database it should be easy as: MyModel.where(created_at: 30.days.ago, destroyable: true).destroy_all)

Upvotes: 4

Related Questions