While this feature has obvious useful implications, what happens when we actually want to remove an item from an SO for security reasons, or just for neatness? Sure we could change the value but that would still leave junk bytes on the user's system. Well Macromedia, God bless 'em, thought of that too. All you do is delete the property from the object within Flash, then flush again and it will be deleted from the SO file on the disk. Thanks to Dave Emberton of ActionScript.com for suggesting this application of delete at 3.35am when I came to him babbling inanely. See how hard we work for you people!

mySO = SharedObject.getLocal("test");
mySO.data.stickAround = "I'll be here for years to come!";
mySO.flush();
delete mySO;
// Reload the SO
mySO = SharedObject.getLocal("test");
delete mySO.data.stickAround;
mySO.flush();
// Delete the SO
delete mySO;
// Load the SO back in once more,
mySO = SharedObject.getLocal("test");
// Scan the SO for values
for (a in mySO.data) {
 trace(a+": "+mySO.data[a]);
}
// Wohoo! No stickAround value!

Macromedia's docs don't show us how to remove the physical file from a user's drive, (although they say it's possible), but we can combine two things we know already to figure it out. Firstly, when we write a SO with no properties in its data child, no file is written. Secondly when we delete a property from the data child and re-write the SO the property is removed from the file on the disk also. So in order to delete the actual file, one need only delete all the properties of the data child. This is probably best done with a for...in loop.

// Make a basic SO
mySO = SharedObject.getLocal("test");
mySO.data.stickAround = "I'll be here for years to come!";
mySO.data.myArray = new Array(1, 2, 3, 4);
mySO.flush();
delete mySO;
// Reload the SO
mySO = SharedObject.getLocal("test");
for (a in mySO.data) {
 delete mySO.data[a];
}
mySO.flush();
// Delete the SO
delete mySO;
// Load the SO back in once more,
mySO = SharedObject.getLocal("test");
// Scan the SO for values
for (a in mySO.data) {
 trace(a+": "+mySO.data[a]);
}
// There are none. There's no file on the drive any more either

It's noteworthy that all the examples I've used in this tutorial cover creating and accessing the SO from the same SWF file. In practice this will rarely be the case, as we like to make our SWF files modular. To access a SO file created by a different SWF file you need to provide additional arguments to the getLocal() method. See this thread in which we show how it's done.

Finally, if you are determined to force your user to accept the SO you can advise them that SOs are required for your application and then run this code:

System.showSettings(1);

which will popup the local settings configuration box (see Figure 2.1) and allow the user to enable/disable SO storage and increase/decrease allocated SO storage space on a general scale.


Figure 2.1 Change Local Storage Settings Prompt

Viola! That's it! Thanks to Macromedia for their good docs on this. I'm off to bed. If this tutorial helped you out please drop me an email and let me know. I'm open to corrections and suggestions as always.

Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash.

NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there.

If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity.
This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it.