Picture of the author

GAURAV VARMA

← BACK TO BLOG

Rails 6.1 allows destroying associations asynchronously


Rails 6.1 introduces support for :destroy_async on associations — enabling deferred deletion of associated records through background jobs.

Why This Matters

Cascading deletions in complex systems can lead to slow requests and timeouts. Consider a model with deeply nested associations — using dependent: :destroy could block the request thread as it recursively deletes records.

Rails 6.1 solves this by introducing:

1has_many :comments, dependent: :destroy_async

Instead of deleting inline, Rails now queues a job to destroy the associated records in the background.

Example

1class Author < ApplicationRecord
2  has_many :books, dependent: :destroy_async
3end
4
5class Book < ApplicationRecord
6  belongs_to :author
7end

Now, when an Author is destroyed, Rails enqueues a background job to remove the associated books.

1Performing ActiveRecord::DestroyAssociationAsyncJob
2  Job ID: d5af82ac-ee53-453d-8736-541ae0f68105
3  Arguments: {
4    owner_model_name: "Author",
5    owner_id: 1,
6    association_class: "Book",
7    association_ids: [1, 2, 3, 4],
8    association_primary_key_column: :id
9  }

The Author record is deleted immediately, and Book records are processed asynchronously — improving UX and system responsiveness.

When to Use

  • Large hierarchies of associated records
  • Performance-sensitive delete operations
  • Avoiding request timeout errors

Keep in mind: callbacks still run, just deferred in a job.

Summary

Rails 6.1’s :destroy_async gives developers a native way to offload costly association deletions into background jobs. It’s a small change with big impact on scalability and performance.

For more details, check the Rails PR #40157.