Can't delete timeline variable

Add a Timeline, and then remove the newly added timeline node using “RemoveUnusedNodes” in the editor tool. The Timeline variable in the blueprint cannot be removed now.

Use this to clear the invalid timeline

void UEditorToolFunctionLibrary::RemoveInvalidTimeline(UBlueprint* Blueprint)
{
	Blueprint->Modify();
	int32 removedNumber = 0;
	
	TArray<TObjectPtr<class UTimelineTemplate>> deleteTimelines;
	for (auto timeLine: Blueprint->Timelines)
	{
		UK2Node_Timeline* TimelineNode = FBlueprintEditorUtils::FindNodeForTimeline(Blueprint, timeLine);
		if (!TimelineNode)
		{
			deleteTimelines.Add(timeLine);
			removedNumber++;
		}
	}
	for (auto deTimeLine : deleteTimelines)
	{
		deTimeLine->Modify();

		Blueprint->Timelines.Remove(deTimeLine);
		deTimeLine->MarkAsGarbage();
	}
	deleteTimelines.Empty();

	if (removedNumber>0)
	{
		FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(Blueprint);
	}

	UE_LOG(LogTemp, Log, TEXT(" --- Removed timelines number: %d "),removedNumber);
}

If you are looking for a solution for 4.27, here it is. And don’t forget to add module UnrealEd in public dependencies

void UV_BlueprintFnLibrary::RemoveInvalidTimeline(UBlueprint* Blueprint)
{
Blueprint->Modify();
int32 removedNumber = 0;

TArray<TSoftObjectPtr<class UTimelineTemplate>> deleteTimelines;
for (auto timeLine: Blueprint->Timelines)
{
	UK2Node_Timeline* TimelineNode = FBlueprintEditorUtils::FindNodeForTimeline(Blueprint, timeLine);
	if (!TimelineNode)
	{
		deleteTimelines.Add(timeLine);
		removedNumber++;
	}
}
for (auto deTimeLine : deleteTimelines)
{
	deTimeLine->Modify();

	Blueprint->Timelines.Remove(deTimeLine.Get());
	deTimeLine->MarkPendingKill();
}
deleteTimelines.Empty();

if (removedNumber>0)
{
	FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(Blueprint);
}

UE_LOG(LogTemp, Log, TEXT(" --- Removed timelines number: %d "),removedNumber);

}