# fluent-booking/2.5.0/app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php

Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution, version 2.5.0. 113 lines.

- Page: https://pluginprobe.com/plugins/fluent-booking/2.5.0/code/app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php
- Raw: https://pluginprobe.com/plugins/fluent-booking/2.5.0/raw/app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php
- Modified: 2026-09-24T14:25:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluent-booking/2.5.0/code/app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php#L10-L20`.

```php
<?php

namespace FluentBooking\App\Hooks\Handlers\CleanupHandlers;

use FluentBooking\App\Models\Booking;
use FluentBooking\App\Models\BookingHost;
use FluentBooking\App\Models\CalendarSlot;
use FluentBooking\App\Models\Availability;
use FluentBooking\App\Models\Meta;
use FluentBooking\Framework\Support\Arr;

class CalenderCleaner
{
    public function register()
    {
        add_action('fluent_booking/before_delete_calendar', [$this, 'handleBeforeDelete']);
    }

    public function handleBeforeDelete($calendar)
    {
        if (empty($calendar)) {
            return;
        }

        Meta::query()
            ->where('object_type', 'Calendar')
            ->where('object_id', $calendar->id)->delete();

        $calendarEvents = CalendarSlot::query()
            ->where('calendar_id', $calendar->id)->get();

        if ($calendarEvents->count()) {
            foreach ($calendarEvents as $event) {
                $eventId = $event->id;
                do_action('fluent_booking/before_delete_calendar_event', $event, $calendar);
                $event->delete();
                do_action('fluent_booking/after_delete_calendar_event', $eventId, $calendar);
            }
        }

        $teamEvents = CalendarSlot::query()
            ->where('calendar_id', '!=', $calendar->id)
            ->where(function ($query) {
                $query->where('event_type', 'round_robin')
                    ->orWhere('event_type', 'collective');
            })
            ->get();

        if ($teamEvents->count()) {
            foreach ($teamEvents as $event) {
                $teamMembers = Arr::get($event->settings, 'team_members', []);
                if (in_array($calendar->user_id, $teamMembers)) {
                    $updatedTeamMembers = array_filter($teamMembers, function ($memberId) use ($calendar) {
                        return $memberId != $calendar->user_id;
                    });
                    $event->settings = [
                        'team_members' => array_values($updatedTeamMembers) ?: [(int)$event->user_id]
                    ];
                    $event->save();

                    $calendarUserId = $calendar->user_id;
                    Booking::query()
                        ->where('event_id', $event->id)
                        ->whereHas('hosts', function ($query) use ($calendarUserId){
                            $query->where('user_id', $calendarUserId);
                        })
                        ->withCount('hosts')
                        ->chunkById(100, function ($bookings) use ($calendarUserId) {
                            $sharedBookingIds = [];

                            foreach ($bookings as $booking) {
                                // A collective booking still happens with its other hosts.
                                if ($booking->hosts_count > 1) {
                                    $sharedBookingIds[] = $booking->id;
                                    continue;
                                }

                                $bookingId = $booking->id;
                                do_action('fluent_booking/before_delete_booking', $booking);
                                $booking->delete();
                                do_action('fluent_booking/after_delete_booking', $bookingId);
                            }

                            if ($sharedBookingIds) {
                                BookingHost::query()->where('user_id', $calendarUserId)->whereIn('booking_id', $sharedBookingIds)->delete();
                            }
                        });
                }
            }
        }

        if ($calendar->type != 'simple') {
            return;
        }

        $availabilitySchedules = Availability::where('object_id', $calendar->user_id)->get();

        if ($availabilitySchedules->isNotEmpty()) {
            foreach ($availabilitySchedules as $schedule) {
                $eventAssociated = CalendarSlot::where('availability_id', $schedule->id)
                    ->where('user_id', '!=', $schedule->object_id)
                    ->count();

                $scheduleId = $schedule->id;
                if ($eventAssociated == 0) {
                    do_action('fluent_booking/before_delete_availability_schedule', $schedule);
                    $schedule->delete();
                    do_action('fluent_booking/after_delete_availability_schedule', $scheduleId);
                }
            }
        }
    }
}
```
