Laravel Tutorials

Reset Rate Limits Dynamically with Laravel's clear Method

Published
Reset Rate Limits Dynamically with Laravel's clear Method image

Laravel's RateLimiter clear method enables dynamic rate limit resets when specific conditions are met, providing flexible control over rate limiting behavior in your applications.

Clear a rate limit programmatically:

use Illuminate\Support\Facades\RateLimiter;

RateLimiter::clear('api-calls:' . $userId);

Here's a practical implementation for a file upload system:

class FileUploadController extends Controller
{
    public function upload(Request $request)
    {
        $key = 'file-upload:' . auth()->id();
        $limiter = RateLimiter::attempt(
            $key,
            $perHour = 10,
            function() use ($request) {
                return FileUpload::create([
                    'user_id' => auth()->id(),
                    'filename' => $request->file('document')->store('uploads'),
                    'size' => $request->file('document')->getSize()
                ]);
            }
        );

        if (!$limiter) {
            return response()->json([
                'error' => 'Upload limit exceeded. Please wait.'
            ], 429);
        }

        return response()->json(['status' => 'File uploaded successfully']);
    }

    public function approveUpload(FileUpload $upload)
    {
        $this->authorize('approve', $upload);
        
        $upload->update(['status' => 'approved']);
        
        RateLimiter::clear('file-upload:' . $upload->user_id);
        
        return response()->json([
            'status' => 'Upload approved',
            'quota_reset' => true
        ]);
    }

    public function resetUserQuotas(User $user)
    {
        RateLimiter::clear('file-upload:' . $user->id);
        RateLimiter::clear('bulk-download:' . $user->id);
        RateLimiter::clear('share-link:' . $user->id);
        
        return back()->with('success', 'Upload quotas reset');
    }
}

The clear method provides a way to programmatically reset rate limits when certain conditions are met, making your rate limiting system more dynamic and user-friendly.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article