---
title: "Code tip: clean up after yourself (and a seltzer review)"
url: "https://aaronfrancis.com/videos/code-tip-clean-up-after-yourself-and-a-seltzer-review"
path: "/videos/code-tip-clean-up-after-yourself-and-a-seltzer-review"
type: video
author: Aaron Francis
published: 2024-07-26
topic: "Laravel & PHP"
keywords: [cleanup callbacks, undo stack, cleanup patterns, resource management, code cleanup, refactoring, poppin off series, code patterns, javascript cleanup, best practices]
youtube_url: "https://www.youtube.com/watch?v=TT7RtrYtuek"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Code tip: clean up after yourself (and a seltzer review)

## Chapters

- Welcome to Poppin Off Series
- Seltzer Overview
- Clean Up After Yourself in Code
- Approach 1: Returning a Callback
- Approach 2: Register an Undo Stack
- Benefits of Both Approaches
- Seltzer Ranking

## Transcript

**Aaron Francis:** Welcome to Poppin' Off. This is a series where I'm gonna pop off about code-specific topics. That could be refactorings, best practices, patterns, whatever it may be. We'll keep it nice and short, scoped down to a single idea, and all the while, we're gonna be drinking something refreshing. Today, we're popping open a Spindrift lemon sparkling water.

**Aaron Francis:** Now, normally, flavored sparkling waters have the mere suggestion of a flavor. It's as if you're drinking a regular water and someone in a room next door shouts a flavor. That's normal sparkling water. However, Spindrift actually has a flavor. No sugars, three calories. We're gonna be drinking the Spindrift. Let's talk about cleaning up after yourself in code.

**Aaron Francis:** This is a Laravel command, but before you leave, and before you leave a comment that tells me how much PHP sucks, I know that it sucks. Everybody has told me that. This applies to every framework, every language. This applies to your life. You've gotta clean up after yourself. Come on, guys. So, here's what we're working with. We're gonna start a process and touch a file, and then we're gonna write some stuff out, and then we're gonna run in an infinite loop until we interrupt the command, at which point it will exit.

**Aaron Francis:** So, if we were to look at this, we can just run app run, and we'll see starting ngrok, touching a file, waiting, and now we're in this infinite loop, and stopping and exiting. This is the command that we're working with. However, we don't ever, uh, delete this file that we touched, and we don't ever make sure that this process that we started is terminated.

**Aaron Francis:** It will likely be terminated when the parent process is terminated, but we should go ahead and clean up after ourselves and make sure that everything is spic and span. So, there are two ways that we can do this. The first one is to return a callback, and the second one is to register an undo stack. Let's start by returning a callback. To do that, we're gonna go into each function and return a callback that undoes itself.

**Aaron Francis:** So, we're gonna assign this process a variable, and then we're gonna return a function that uses that process, and we can say, if process running, then process signal, and we're gonna send the sig int, so we're going to interrupt. And we'll say, uh, in fact, let's say down here, this line, stopping ngrok.

**Aaron Francis:** And we can return that, and now this start process returns a stop process. God, that just feels so clean, doesn't it? So then, down here, we can say stop process, and then let's go do the same thing in touch file. So we'll come down here, and we will return a function that says at unlink.

**Aaron Francis:** And so if the file is gone, we actually don't care, uh, but we do wanna make sure that it is gone. So we'll say unlink that file, and we'll just put, uh, this line, unlinking file. And that should be good enough. And now we have a, uh, destroy file. Could say delete, but destroy sounds a lot more intense, so I'm gonna go with destroy.

**Aaron Francis:** And down here. So now, what we've done is each process returns its own undo function, such that if we were to run this in here, you should see it's starting, it's running, running, running, and we interrupt it. Boom. Stopping ngrok, unlinking the file, and exiting. The second way that we can do this is with like a, uh, like a callback stack.

**Aaron Francis:** So let's say protected, um, array cleanup. Uh, yeah, we'll just call it cleanup callbacks to be extra explicit here. And I kinda like this because you could have potentially, a, a process could potentially add many callbacks to the stack, which, you know, I kinda like. You could, of course, wrap all that up into a single function, but I kinda like this.

**Aaron Francis:** So instead of returning, we are going to just push this onto the stack. So this callback, this cleanup callbacks equals this new function. And we can come down here. Instead of returning that guy, we'll do that. And then we just need to write, uh, we just need to write a cleanup function. I'll just put it up here for giggles. Public function cleanup.

**Aaron Francis:** You could probably just inline this, but we're already here, so for each this cleanup callbacks as CB, and then we can just say CB. Now all you have to do is say this cleanup, and we should get the exact same output that we were getting before. So we see starting, touching, waiting, and stopping, unlinking, exiting.

**Aaron Francis:** Here's what I like about this. Regardless of which implementation you choose, whether that's returning the callback function or pushing onto a callback stack, it keeps the action and the reversal of the action very close together. So one method is responsible for declaring how to instantiate and how to tear down, even if the tear down comes in a different place and much, much later.

**Aaron Francis:** I like this, that it keeps it all co-located together. Makes me feel good. Also makes me feel nice that I'm sure we've cleaned up after ourselves at the end. We're not leaving processes running or files hanging around. Now, normally, I'm gonna rank, I'm gonna rank what we're drinking. This is the first one, so we're gonna rank Spindrift lemon at the top.

**Aaron Francis:** This is the number one right now. There will be a lot more, so I don't know if it's gonna hang on to the crown, but for now, way to go, Spindrift lemon being number one. Please stay tuned for more Poppin' Off. Until the next time, see ya.

---

## Related on aaronfrancis.com

- [Code tip: Don't undo what you haven't done](/videos/code-tip-dont-undo-what-you-havent-done) *(Video · 2024-08-02 · shared: poppin off series, refactoring, code patterns)*
- [Clean up after yourself](/writing/clean-up-after-yourself) *(Article · 2024-09-04 · shared: resource management, undo stack)*
- [You Do Have To Care w/ John Drexler & Daniel Coulbourne](/podcast/you-do-have-to-care-w-john-drexler-daniel-coulbourne-abdb6670) *(Podcast · 2026-06-23 · shared: best practices)*
- [Move Your Feet](/podcast/move-your-feet-d736ee8f) *(Podcast · 2015-10-06 · shared: best practices)*
- [Escaping the Underclass (Part 2)](/videos/escaping-the-underclass) *(Video · 2026-05-06 · shared: refactoring)*

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `cleanup callbacks` ([C](/llms/keywords/c.txt)), `undo stack` ([T-Z](/llms/keywords/t-z.txt)), `resource management` ([Q-R](/llms/keywords/q-r.txt)), `poppin off series` ([O-P](/llms/keywords/o-p.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
