This story originally featured on Popular Science.

There are few things more soul-crushing than losing that last photo of grandma, or videos of your child’s first steps. But SD cards can be fussy, and occasionally those photos get corrupted or accidentally deleted. And it’s not quite possible to recreate those photos after the moment’s passed.

The good news is that if you still have that SD card, you still have hope.

How file recovery works

This may come as a shock to you, but when you delete a file from an SD card (or hard drive, or flash drive) that data isn’t erased right away—instead, your computer just marks that space as available for re-writing. Even though you can’t see the files, they’re still there until your camera (or computer) overwrites them with something else. That means that if you haven’t taken too many new photos, you might be able to get those old ones back.

So if you lost or accidentally deleted files off an SD card, the first thing you must do is stop using it. Don’t take any more pictures—remove it from your camera and set it aside. If you keep snapping away, you might overwrite the photos you’ve lost, and then you really won’t be able to get them back.

[Related: The fastest SD cards for any device.]

Be ready to pay a little money if things get tough. We’ll go through two free SD card recovery programs here, but if they can’t find what you’re looking for, it may be worth trying a paid option instead—sometimes one program can root out files that others missed. You’re not going at it blind, though—most paid programs will let you scan the drive for free, at which point you can pony up to actually recover the files. A few bucks is usually a small price to pay for those irreplaceable family photos.

Let’s get started.

Windows: Recover your files with Recuva

Photos deleted from your memory card? Here’s how to recover them.
As long as they have not been overwritten, your files might still be in there. Whitson Gordon

Recuva is a free tool that can be a little finicky, but it’s a good place to start. Install Recuva like you would any other Windows program and start it up. If, for some reason, it doesn’t launch, it may be conflicting with a driver on your PC or an antivirus program—this is a somewhat common issue, and I was able to get it to launch by booting into Safe Mode. (Just hold Shift while restarting the computer, head to Troubleshoot > Advanced Options > Startup Settings, and restart into Safe Mode with Networking.) I also had to plug my SD card in using a USB card reader, rather than my laptop’s built-in SD card slot, though your mileage may vary.

Recuva’s Wizard will attempt to make the scan a bit speedier by asking you what you’re looking for. The first time you go through, I recommend choosing Pictures on My Media Card or iPod, but you can choose All Files from All Drives if you prefer to catch everything humanly possible—it’ll just take longer.

Recuva will then present you with a list of files it found. In my case, it found about 13 deleted photos I’d taken for work a couple years ago, and I was able to recover all but one of them (which had probably been partially overwritten with new data at some point). If the software found a lot of files, note that checking each box can be a tedious task, so I recommend selecting the first photo, holding Shift, then selecting the last photo to highlight them all. Then, right-click on a photo and choose Check Highlighted to mark every photo for recovery. Click the Recover button to find a place for them on your computer’s hard drive. Do not recover them to the SD card, or you may overwrite the files before they can be recovered.

If you don’t find everything the first time, try re-running Recuva and doing a Deep Scan—it’ll take longer, but you might get lucky. When I ran a deep scan, Recuva found about 35 photos from years ago that it didn’t find in the initial scan.

Mac: Recover your files with LazeSoft Mac Data Recovery

Photos deleted from your memory card? Here’s how to recover them.
If you want to really dive into the depths of your SD card, make yourself a cup of coffee and go for the deep scan. Whitson Gordon

If you’re a Mac user, I recommend starting with a freeware program called LazeSoft Mac Data Recovery. It isn’t pretty, but it’s completely free, and worked remarkably well in my tests. Install the program as you would any other, then start it up and select either a Fast Scan or a Deep Scan. (A deep scan will find more, but will take longer.) Select your SD card when prompted, and let it do its thing.

[Related: Fast and reliable memory card readers.]

When LazeSoft is done scanning, you’ll have a list of folders in the left sidebar you can browse through. It doesn’t show thumbnails, so it can take some time to find the photos you’re looking for. Once you do, you can check them off and click on Save Files along the top to recover them to your hard drive. Again, remember not to recover them to the SD card, or you may overwrite the files before they can be recovered. Be sure to check through all the folders in the sidebar to make sure you catch every photo that’s available for recovery.

What to do if you don’t find what you’re looking for

Photos deleted from your memory card? Here’s how to recover them.
Free file-recovery programs will only get you so far. To unleash your SD card’s entire history, you’ll have to pay. Whitson Gordon

Here’s the thing: The above programs are great first efforts, but they’re free for a reason. If you aren’t finding everything you want to recover, it’s time to try something a bit heftier, like Disk Drill Pro ($89, for Windows and Mac, including the latest Big Sur OS), EaseUS Data Recovery Wizard ($70 per month or $100 per year, for Windows; $90 per month or $120 per year, for Mac), or Ontrack EasyRecovery ($79 per year, for Windows and Mac). When I tested these programs, I found 200 photos—a far cry from the 51 found in Recuva’s deep scan—plus a couple of videos, taken so long ago I barely remembered them. All three options will scan your drive for free, but will only recover between 500MB and 1GB of data before you have to pay. Honestly, that’s a pretty good deal. Try one or all of them before you pony up to make sure you get the maximum amount of recovered photos possible.

If they still can’t do the trick, you may be out of luck. But if those lost photos are incredibly important to you, a data recovery service like DriveSavers or LC Technology may be able to recover files that consumer-level software can’t—at a cost of hundreds or even over a thousand dollars. So you’ll have to decide if recovering those photos is actually worth potentially spending that amount of money.

Whether you’re able to recover your photos or not, remember that the story doesn’t end here—you’ll also want to do everything in your power from preventing this in the future. Copy your photos to your PC early and often, properly eject your SD card before removing it from your PC, and have a good backup strategy in place, so those memories never disappear.