# Fix Jellyfin Marker Conflict Error

**Error:** `Expected to find only .jellyfin-data but found marker for .jellyfin-config`  
**Cause:** Config and data directories point to same location  
**Solution:** Delete old markers or fix directory configuration

---

## Quick Fix: Delete Old Markers

```powershell
# Remove conflicting marker files
Remove-Item "C:\ProgramData\jellyfin\.jellyfin-config" -Force
Remove-Item "C:\ProgramData\jellyfin\log\.jellyfin-log" -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\jellyfin\plugins\.jellyfin-plugin" -Force -ErrorAction SilentlyContinue

# Or remove entire directory and start fresh
Remove-Item "C:\ProgramData\jellyfin" -Recurse -Force

# Then restart Jellyfin
```

---

## Root Cause

### What Are Marker Files?

Jellyfin creates hidden marker files (`.jellyfin-*`) to identify:
- `.jellyfin-config` - Config directory
- `.jellyfin-data` - Data directory  
- `.jellyfin-log` - Log directory
- `.jellyfin-plugin` - Plugin directory
- `.jellyfin-cache` - Cache directory

### The Problem

**Your current state:**
```
C:\ProgramData\jellyfin\
  ├── .jellyfin-config      ← From old installation
  ├── .jellyfin-data         ← Trying to create
  ├── log\
  │   └── .jellyfin-log
  └── plugins\
      └── .jellyfin-plugin
```

**What happened:**
1. Previous installation used `C:\ProgramData\jellyfin\` as config dir
2. Now trying to use same folder as data dir
3. Sanity check fails - can't be both!

---

## Solution 1: Clean Start (Recommended) ✅

### Step 1: Backup Your Data (If Needed)

```powershell
# If you have existing data
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
Copy-Item "C:\ProgramData\jellyfin" "C:\ProgramData\jellyfin-backup-$timestamp" -Recurse -Force
```

### Step 2: Delete Old Installation Data

```powershell
# Remove everything
Remove-Item "C:\ProgramData\jellyfin" -Recurse -Force

# Verify
Test-Path "C:\ProgramData\jellyfin"
# Should return: False
```

### Step 3: Start Jellyfin

```bash
cd E:\Projects\pgsql-jellyfin\lib\Release\net11.0
dotnet jellyfin.dll

# Jellyfin will:
# 1. Create C:\ProgramData\jellyfin\
# 2. Create proper marker files
# 3. Initialize fresh configuration
```

---

## Solution 2: Use Separate Directories

### Configure Different Paths

**Create:** `C:\ProgramData\jellyfin\startup.json`

```json
{
  "DataPath": "C:\\ProgramData\\jellyfin\\data",
  "ConfigPath": "C:\\ProgramData\\jellyfin\\config",
  "LogPath": "C:\\ProgramData\\jellyfin\\log",
  "CachePath": "C:\\ProgramData\\jellyfin\\cache"
}
```

### Then Clean Up

```powershell
# Remove old markers
Remove-Item "C:\ProgramData\jellyfin\.jellyfin-*" -Force -ErrorAction SilentlyContinue

# Start Jellyfin
dotnet jellyfin.dll --datadir "C:\ProgramData\jellyfin\data"
```

---

## Solution 3: Use Custom Data Directory

### Start with Custom Path

```bash
# Use project folder for development
cd E:\Projects\pgsql-jellyfin\lib\Release\net11.0
dotnet jellyfin.dll --datadir "E:\Projects\pgsql-jellyfin\dev-data"

# Or use a different system directory
dotnet jellyfin.dll --datadir "C:\jellyfin-dev\data"
```

---

## PowerShell Quick Fix Script

```powershell
# Quick cleanup script
Write-Host "Fixing Jellyfin marker conflict..." -ForegroundColor Cyan

# Backup existing data
if (Test-Path "C:\ProgramData\jellyfin\library.db") {
    $backup = "C:\ProgramData\jellyfin-backup-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
    Write-Host "Backing up to: $backup" -ForegroundColor Yellow
    Copy-Item "C:\ProgramData\jellyfin" $backup -Recurse -Force
}

# Remove conflicting markers
Write-Host "Removing conflicting markers..." -ForegroundColor Cyan
Remove-Item "C:\ProgramData\jellyfin\.jellyfin-*" -Force -ErrorAction SilentlyContinue

# Clean subdirectory markers
Get-ChildItem "C:\ProgramData\jellyfin" -Recurse -Filter ".jellyfin-*" | Remove-Item -Force

Write-Host "✅ Markers removed - restart Jellyfin" -ForegroundColor Green
```

---

## Understanding the Error

### Code Flow

**File:** `Emby.Server.Implementations/AppBase/BaseApplicationPaths.cs`

```csharp
// Line 97: Check all paths
public virtual void MakeSanityCheckOrThrow()
{
    CreateAndCheckMarker(ConfigurationDirectoryPath, "config");  // Creates .jellyfin-config
    CreateAndCheckMarker(LogDirectoryPath, "log");               // Creates .jellyfin-log
    CreateAndCheckMarker(PluginsPath, "plugin");                 // Creates .jellyfin-plugin
    CreateAndCheckMarker(ProgramDataPath, "data");               // Creates .jellyfin-data ← ERROR HERE
    CreateAndCheckMarker(CachePath, "cache");                    // Creates .jellyfin-cache
    CreateAndCheckMarker(DataPath, "data");                      // Creates .jellyfin-data
}

// Line 115-130: Check for conflicts
private void CheckOrCreateMarker(string path, string markerName, bool recursive = false)
{
    // Find OTHER markers in this directory
    otherMarkers = GetMarkers(path, recursive)
        .FirstOrDefault(e => !Path.GetFileName(e).Equals(markerName));
    
    // If found other markers → ERROR
    if (otherMarkers is not null)
    {
        throw new InvalidOperationException(
            $"Expected to find only {markerName} but found marker for {otherMarkers}.");
    }
    
    // Create marker file
    File.Create(Path.Combine(path, markerName));
}
```

**What's happening:**
1. Code tries to create `.jellyfin-data` in `C:\ProgramData\jellyfin\`
2. Finds existing `.jellyfin-config` there
3. Throws error because one folder can't be both config AND data

---

## Why This Happens

### Common Causes

1. **Previous Installation** - Old Jellyfin used that directory
2. **Configuration Mix-up** - Paths not configured correctly
3. **Upgrade Issue** - Migrating from different version
4. **Manual Configuration** - Manually set conflicting paths

### Your Situation

Based on the error, your paths are likely:
- **ConfigPath:** `C:\ProgramData\jellyfin` (has `.jellyfin-config`)
- **DataPath:** `C:\ProgramData\jellyfin` (trying to create `.jellyfin-data`)

**Problem:** Same directory for both!

---

## Recommended Fix

### Quick 3-Step Fix

```powershell
# Step 1: Stop Jellyfin if running
Stop-Process -Name "jellyfin" -Force -ErrorAction SilentlyContinue

# Step 2: Remove marker files
Remove-Item "C:\ProgramData\jellyfin\.jellyfin-config" -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\jellyfin\.jellyfin-data" -Force -ErrorAction SilentlyContinue
Get-ChildItem "C:\ProgramData\jellyfin" -Filter ".jellyfin-*" -Recurse | Remove-Item -Force

# Step 3: Restart Jellyfin
cd E:\Projects\pgsql-jellyfin\lib\Release\net11.0
dotnet jellyfin.dll
```

**This will:**
- Remove conflicting markers
- Let Jellyfin recreate them correctly
- Use default directory structure

---

## If Problem Persists

### Check Your Startup Configuration

1. **Check for startup.json:**
   ```powershell
   Get-Content "C:\ProgramData\jellyfin\startup.json" -ErrorAction SilentlyContinue
   ```

2. **Check environment variables:**
   ```powershell
   Get-ChildItem Env: | Where-Object { $_.Name -like "*JELLYFIN*" }
   ```

3. **Check command line args:**
   - Are you passing `--datadir` or `--configdir`?

---

## Prevention

### Proper Directory Structure

**Windows default:**
```
C:\ProgramData\jellyfin\
  ├── config\           ← Config files (.jellyfin-config marker)
  ├── data\             ← Database files (.jellyfin-data marker)
  ├── log\              ← Log files (.jellyfin-log marker)
  ├── cache\            ← Cache files (.jellyfin-cache marker)
  └── plugins\          ← Plugins (.jellyfin-plugin marker)
```

**Each subdirectory has its own marker** - no conflicts!

---

## Files to Check/Delete

```powershell
# List all markers
Get-ChildItem "C:\ProgramData\jellyfin" -Filter ".jellyfin-*" -Recurse | 
    Select-Object FullName | 
    ForEach-Object { Write-Host $_.FullName }
```

Expected after fix:
```
C:\ProgramData\jellyfin\.jellyfin-data
C:\ProgramData\jellyfin\config\.jellyfin-config
C:\ProgramData\jellyfin\log\.jellyfin-log
C:\ProgramData\jellyfin\cache\.jellyfin-cache
C:\ProgramData\jellyfin\plugins\.jellyfin-plugin
```

---

## Summary

**Error:** InvalidOperationException - conflicting markers  
**Cause:** Multiple marker files in same directory  
**Fix:** Delete conflicting markers  
**Prevention:** Use separate subdirectories for each purpose

**Quick command:**
```powershell
Remove-Item "C:\ProgramData\jellyfin\.jellyfin-config" -Force
```

Then restart Jellyfin! ✅
