<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Roland Levy</title>
    <description>The latest articles on DEV Community by Roland Levy (@rolandjlevy).</description>
    <link>https://dev.to/rolandjlevy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4190693%2Ffb3500ee-0856-4953-a487-222dd91fa9f8.jpg</url>
      <title>DEV Community: Roland Levy</title>
      <link>https://dev.to/rolandjlevy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rolandjlevy"/>
    <language>en</language>
    <item>
      <title>css-ghost: find and delete orphaned CSS selectors with zero config</title>
      <dc:creator>Roland Levy</dc:creator>
      <pubDate>Sat, 17 Oct 2026 16:23:09 +0000</pubDate>
      <link>https://dev.to/rolandjlevy/css-ghost-find-and-delete-orphaned-css-selectors-with-zero-config-4pb8</link>
      <guid>https://dev.to/rolandjlevy/css-ghost-find-and-delete-orphaned-css-selectors-with-zero-config-4pb8</guid>
      <description>&lt;p&gt;You've been there. You rename a component, move a file, or quietly migrate from one styling approach to another – and the old CSS stays behind. Nobody complains, the build doesn't break, and the dead selectors quietly accumulate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/css-ghost" rel="noopener noreferrer"&gt;css-ghost&lt;/a&gt; is a small Node.js CLI tool I built to solve that problem. Point it at a folder and it tells you every CSS class defined in your style files that isn't referenced anywhere in your components.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8s7iyuwijrcwqgj7qm4t.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8s7iyuwijrcwqgj7qm4t.gif" alt="css-ghost terminal demo" width="800" height="602"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What it does
&lt;/h2&gt;

&lt;p&gt;css-ghost scans your style files (&lt;code&gt;.css&lt;/code&gt;, &lt;code&gt;.module.css&lt;/code&gt;, &lt;code&gt;.scss&lt;/code&gt;, &lt;code&gt;.sass&lt;/code&gt;, &lt;code&gt;.less&lt;/code&gt;, MUI &lt;code&gt;makeStyles&lt;/code&gt;, vanilla-extract), extracts every class selector, then cross-references them against every component file it can find (&lt;code&gt;.js&lt;/code&gt;, &lt;code&gt;.jsx&lt;/code&gt;, &lt;code&gt;.ts&lt;/code&gt;, &lt;code&gt;.tsx&lt;/code&gt;, &lt;code&gt;.vue&lt;/code&gt;, &lt;code&gt;.svelte&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Anything defined but never actually used gets reported as an orphan – with its line number and the full CSS rule printed inline, so you can see what it does before deciding to delete it.&lt;/p&gt;

&lt;p&gt;It also catches &lt;strong&gt;orphaned CSS custom properties&lt;/strong&gt; (&lt;code&gt;--my-token: value&lt;/code&gt; defined but never consumed via &lt;code&gt;var(--my-token)&lt;/code&gt; anywhere in the codebase), reported in a separate 🔮 section.&lt;/p&gt;




&lt;h2&gt;
  
  
  Run it now
&lt;/h2&gt;

&lt;p&gt;No install required:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx css-ghost ./src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or install globally for repeated use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; css-ghost
css-ghost ./src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Interactive folder picker
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;css-ghost&lt;/code&gt; with no arguments and an interactive directory picker opens. Navigate with &lt;code&gt;↑↓&lt;/code&gt; (or &lt;code&gt;j/k&lt;/code&gt;), press &lt;code&gt;Enter&lt;/code&gt; or &lt;code&gt;→&lt;/code&gt; to go into a folder, &lt;code&gt;←&lt;/code&gt; to go back up, &lt;code&gt;[a]&lt;/code&gt; to add the current directory to the scan, then &lt;code&gt;[d]&lt;/code&gt; when done:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;css-ghost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful when you're not sure which subfolder to target, or want to scan multiple directories in one pass.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dynamic class detection
&lt;/h2&gt;

&lt;p&gt;If a component accesses classes dynamically – &lt;code&gt;styles[variable]&lt;/code&gt;, &lt;code&gt;styles[\&lt;/code&gt;${variant}&lt;code&gt;]&lt;/code&gt; – css-ghost can't resolve the class name statically. Rather than silently skip those files or report false positives, it flags them with a ⚠️ warning per file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✅ src/components/Alert/Alert.module.css

   ⚠️  Dynamic class references detected – results may be incomplete.
   Dynamic references in: src/components/Alert.jsx

   👻 .hidden (line 30)
      .hidden { display: none; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All statically resolvable patterns are still analysed: string literals, ternaries, &lt;code&gt;cx({ active: isOn })&lt;/code&gt; object keys, array literals, and both branches of ternaries.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deleting orphans
&lt;/h2&gt;

&lt;p&gt;Once you're happy with the report, pass &lt;code&gt;--delete&lt;/code&gt; to remove every flagged selector from its source file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;css-ghost ./src &lt;span class="nt"&gt;--delete&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;css-ghost prints the full report first, lists what it's about to delete, then asks for confirmation. Pass &lt;code&gt;--yes&lt;/code&gt; to skip the prompt in CI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;css-ghost ./src &lt;span class="nt"&gt;--delete&lt;/span&gt; &lt;span class="nt"&gt;--yes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Tip: commit or stash first. There's no undo.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Other useful flags
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--ghost-trail&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Show the last git commit that touched each orphan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--strict&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Exit with code 1 when orphans exist (CI / pre-commit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--install-hook ./src&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Write a pre-commit hook that blocks commits with orphans&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--output &amp;lt;path&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Custom path for the markdown report&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--ai&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append a plain-English AI summary (needs free OpenRouter key)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Ghost trail
&lt;/h2&gt;

&lt;p&gt;Pass &lt;code&gt;--ghost-trail&lt;/code&gt; to fetch the last git commit that changed each orphan's occurrence count:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;css-ghost ./src &lt;span class="nt"&gt;--ghost-trail&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="err"&gt;👻&lt;/span&gt; &lt;span class="nc"&gt;.iconRight&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;line&lt;/span&gt; &lt;span class="err"&gt;28&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="err"&gt;🕵️&lt;/span&gt;  &lt;span class="nt"&gt;b5c6efb&lt;/span&gt; &lt;span class="err"&gt;·&lt;/span&gt; &lt;span class="nt"&gt;Roland&lt;/span&gt; &lt;span class="nt"&gt;Levy&lt;/span&gt; &lt;span class="err"&gt;·&lt;/span&gt; &lt;span class="err"&gt;3&lt;/span&gt; &lt;span class="nt"&gt;weeks&lt;/span&gt; &lt;span class="nt"&gt;ago&lt;/span&gt; &lt;span class="err"&gt;–&lt;/span&gt; &lt;span class="s1"&gt;"Refactor Button component"&lt;/span&gt;
      &lt;span class="nc"&gt;.iconRight&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Handy for tracking down when and why a class went dead.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pre-commit hook
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;css-ghost &lt;span class="nt"&gt;--install-hook&lt;/span&gt; ./src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Writes &lt;code&gt;.git/hooks/pre-commit&lt;/code&gt; (executable, &lt;code&gt;chmod 755&lt;/code&gt;) that blocks commits whenever orphaned selectors exist. Remove it with &lt;code&gt;rm .git/hooks/pre-commit&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  How is it different from PurgeCSS?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/purgecss" rel="noopener noreferrer"&gt;PurgeCSS&lt;/a&gt; is a &lt;strong&gt;build-time bundler&lt;/strong&gt; – it hooks into webpack or PostCSS to strip unused CSS from your &lt;em&gt;production output&lt;/em&gt;. It's designed for frameworks like Tailwind or Bootstrap where you use a fraction of the utility classes.&lt;/p&gt;

&lt;p&gt;css-ghost is a &lt;strong&gt;source code auditor&lt;/strong&gt; – it finds dead selectors in your &lt;em&gt;actual codebase&lt;/em&gt;, with no build step and no config file.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;PurgeCSS&lt;/th&gt;
&lt;th&gt;css-ghost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Works on source files&lt;/td&gt;
&lt;td&gt;❌ Build output only&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Zero config, no build step&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CSS custom property orphans&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Git history per orphan&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pre-commit hook&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deletes dead code for you&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Safe with dynamic classes&lt;/td&gt;
&lt;td&gt;❌ Silently removes them&lt;/td&gt;
&lt;td&gt;✅ Flags with ⚠️&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The two tools are complementary – PurgeCSS for trimming framework output at build time, css-ghost for auditing your own hand-written CSS throughout development.&lt;/p&gt;




&lt;p&gt;The source is on &lt;a href="https://github.com/rolandjlevy/css-ghost" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; and the package is on &lt;a href="https://www.npmjs.com/package/css-ghost" rel="noopener noreferrer"&gt;npm&lt;/a&gt;. PRs and issues welcome.&lt;/p&gt;

</description>
      <category>css</category>
      <category>node</category>
      <category>tooling</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
